Reputation: 8084
I am trying a batchwrite in DynamoDB but it fails.
TableName - td_notes_learn
PK user_id - String
SK datetime - Number
My attempt:
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.batchWrite(
{
RequestItems: {
td_notes_learn: [
{
DeleteRequest: {
Key: {
user_id: "D",
datetime: 5
}
},
PutRequest: {
Item: {
user_id: "G",
datetime: 5,
content: "HELLO WORLD"
}
}
}
]
}
},
(err, data) => {
if (err) {
console.log("Error found" + err);
} else {
console.log(data);
}
}
);
Exception :
Error foundValidationException: Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes
Also, in the same code, if I run the DeleterRequest
and PutRequest
request individually by commenting the other one, the code works fine, error only occurs if I run the together.
Upvotes: 7
Views: 7258
Reputation: 8084
Looks like all the requests needs to be a separate json object and the issue in my code was that all requests was in one json object. Below is the working code.
docClient.batchWrite(
{
RequestItems: {
td_notes_learn: [
{
DeleteRequest: {
Key: {
user_id: "D",
datetime: 5
}
}
}, // WAS MISSING
{ // WAS MISSING
PutRequest: {
Item: {
user_id: "G",
datetime: 5,
content: "HELLO WORLD"
}
}
} // WAS MISSING
]
}
},
(err, data) => {
if (err) {
console.log("Error found" + err);
} else {
console.log(data);
}
}
);
Upvotes: 8