[issue] AWS SDK javascript v3 DynamoDb Client BatchWriteItemCommand

0

Hello,

my code leads to this error: Cannot read properties of undefined (reading '0') in AWS Lambda (nodeJs 18).

Here comes the related code:

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;
    }
}

This code creates for instance those params:

{
    "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"
                    }
                }
            }
        ]
    }
}

I suspect that I am supposed to write Json with type specification but I can't figure how to do it.

Thank you for your help.

gefragt vor einem Jahr2988 Aufrufe
1 Antwort
3
Akzeptierte Antwort

You need to use a higher level client, such as the Document Client. Try instantiate your client like below:

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)
        );

This should allow you to work with native JSON with no issue.

Be sure you have installed the @aws-sdk/lib-dynamodb if you do not have the entire @aws-sdk package installed.

An example can be seen here

profile pictureAWS
EXPERTE
beantwortet vor einem Jahr

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen