【以下的问题经过翻译处理】 你好,
我的代码导致了这个错误: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,但我无法弄清楚如何做。
谢谢你的帮助。