[问题]: AWS SDK javascript v3 DynamoDb客户端 BatchWriteItemCommand

0

【以下的问题经过翻译处理】 你好,

我的代码导致了这个错误:AWS Lambda中无法读取未定义的属性(读取 '0') (nodeJs 18)。

以下是相关的代码: ‘’‘ import { DynamoDBClient, BatchWriteItemCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({ region: "eu-west-1" });

async function addBuildingRoomEvents(events) {

try {

    const exp = Math.round((Date.now() + (2 * 60 * 60) * 1000) / 1000); // seconds

    let putRequests = [];

    for (const event of events) {

        const item = {
            timestamp: event.timestamp,
            uuid: generateUUID(),
            building_room_uuid: event.buildingRoomUuid,
            building_uuid: event.buildingUuid,
            expiration_timestamp: exp,
            event_type: event.eventType,
            event_attribute_name: event.eventAttributeName,
            event_attribute_value: event.eventAttributeValue.toString(),
            hvac_uuid: event.itemUuid
        }

        const putRequest = { PutRequest: { Item: item } };
        putRequests.push(putRequest);
    }


    const params = {

        RequestItems: {
            'event_building_room': putRequests
        }
    };

    console.log(`[addBuildingRoomEvents] params: ${JSON.stringify(params)}`);

    const response = await client.send(
        new BatchWriteItemCommand(params)
    );

    return response;

} catch (err) {

    throw err;
}

} ’‘’

这段代码例如创建了以下参数: ‘’‘ { "RequestItems": { "event_building_room": [ { "PutRequest": { "Item": { "timestamp": 1676575456197, "uuid": "ff1493f1-737a-44e70-ya96a-afc30133e5cf", "building_room_uuid": "226c0c82-86ac-445f8-yb1de-3525366f4d61", "building_uuid": "348af157-2177-4bb3-b322-cf8ff2f54d11", "expiration_timestamp": 1676582656, "event_type": "request", "event_attribute_name": "desired_temperature", "event_attribute_value": "10", "hvac_uuid": "e1858b08-0258-4c71-bf9a-d79a1b2de523" } } }, { "PutRequest": { "Item": { "timestamp": 1676575456197, "uuid": "ecaccce5-8c10-443ee-ybbf9-adab7aaeb7fd", "building_room_uuid": "226c0c82-86ac-445f8-yb1de-3525366f4d61", "building_uuid": "348af157-2177-4bb3-b322-cf8ff2f54d11", "expiration_timestamp": 1676582656, "event_type": "request", "event_attribute_name": "mode", "event_attribute_value": "heat", "hvac_uuid": "e1858b08-0258-4c71-bf9a-d79a1b2de523" } } }, { "PutRequest": { "Item": { "timestamp": 1676575456197, "uuid": "2f65a6ee-2dec-44153-ya904-8317364ef869", "building_room_uuid": "226c0c82-86ac-445f8-yb1de-3525366f4d61", "building_uuid": "348af157-2177-4bb3-b322-cf8ff2f54d11", "expiration_timestamp": 1676582656, "event_type": "request", "event_attribute_name": "power", "event_attribute_value": "false", "hvac_uuid": "e1858b08-0258-4c71-bf9a-d79a1b2de523" } } } ] } } ’‘’

我怀疑我应该使用类型规范来编写Json,但我无法弄清楚如何做。

谢谢你的帮助。

profile picture
专家
已提问 8 个月前33 查看次数
1 回答
0

【以下的回答经过翻译处理】 您需要使用更高级别的客户端,例如“Document Client”。尝试像下面这样实例化您的客户端:

import { DynamoDBClient, } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, BatchWriteCommand} from "@aws-sdk/lib-dynamodb";

const ddb = new DynamoDBClient({ region: "eu-west-1" });
const client = DynamoDBDocumentClient.from(ddb);



...
...
const response = await client.send(
            new BatchWriteCommand(params)
        );

这应该可以让您无问题地使用原生JSON。

请确保您已安装了“@aws-sdk/lib-dynamodb”,如果您没有安装整个“@aws-sdk”包。

示例代码可以在此处查看:https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/dynamodb/src/movies/writeData.js

profile picture
专家
已回答 8 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则