I'm sending a TransactWriteItemCommand to DynamoDB using AWS-SDK v3 in nodejs. I'm using just one PUT command.
I'm getting a TypeError (see below).
I can`t find the bug, everything looks good to me.
Please help.
const {
DynamoDBClient,
TransactWriteItemsCommand,
} = require('@aws-sdk/client-dynamodb');
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb');
let options = {
maxRetries: 3,
httpOptions: {
timeout: 5000,
},
};
const client = new DynamoDBClient(options)
const input = {
TransactItems:[
{
Put: {
TableName: "my-table-name",
Item: {
PK: "myPK",
SK: "mySK",
name: "aname",
code: "acode",
price: 200,
createdAt: "2023-11-07T13:36:12.045Z",
updatedAt: "2023-11-07T13:36:12.045Z"
}
}
}
]
}
const ddbDocClient = DynamoDBDocumentClient.from(client);
ddbDocClient.send(new TransactWriteItemsCommand(input))
Here are the logs:
TypeError: Cannot read properties of undefined (reading '0')
at AttributeValue.visit (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/models/models_0.js:632:40)
at se_AttributeValue (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:2948:38)
at /var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:3336:20
at Array.reduce (<anonymous>)
at se_PutItemInputAttributeMap (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:3332:34)
at Item (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:3311:22)
at applyInstruction (/var/task/node_modules/@smithy/smithy-client/dist-cjs/object-mapping.js:73:33)
at take (/var/task/node_modules/@smithy/smithy-client/dist-cjs/object-mapping.js:44:9)
at se_Put (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:3307:37)
at Put (/var/task/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:3479:21),
response: null
You have 2 issues in your code:
client
, but then proceed to call the API using an unknown client called: ddbDocClient
.Import the document client, where you can continue to use native JSON
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, TransactWriteCommand } = require("@aws-sdk/lib-dynamodb");
let options = {
maxRetries: 3,
httpOptions: {
timeout: 5000,
},
};
const client = new DynamoDBClient(options);
const docClient = DynamoDBDocumentClient.from(client);
const input = {
TransactItems: [
{
Put: {
TableName: "Test",
Item: {
PK: "myPK",
SK: "mySK",
name: "aname",
code: "acode",
price: 200,
createdAt: "2023-11-07T13:36:12.045Z",
updatedAt: "2023-11-07T13:36:12.045Z"
}
}
}
]
}
docClient.send(new TransactWriteCommand(input))
Change your code to :
client.send(new TransactWriteItemsCommand(input))
While doing that, you will also have to change your syntax to the low level client and add type descriptors.
More info on the differences of clients: Exploring DynamoDB Clients