Abdul Manaf Abdul Manaf - 1 month ago 26
Node.js Question

dynamo db error on update expression using node.js

i need to update a list on my aws-dynamo database. i have created a table with partition key : email. then iam insert some email id to the table successfully. Now my table like this

email



manaf1@gmail.com

manaf2@gmail.com

manaf3@gmail.com

Then, i tried to update the table with new key "details" and its value is a list. this is my code

var AWS = require("aws-sdk");

var params =
{
TableName: "manaftable1",
Key: { email: "manaf1@gmail.com" },
UpdateExpression: "set #details = list_append (#details, :detailsinput)",
ExpressionAttributeNames: {
"#details": "details"
},
ExpressionAttributeValues: {
":detailsinput":{ "id": "1","mob": "978956" }
}
};


var docClient = new AWS.DynamoDB.DocumentClient();

docClient.update(params, function (err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});


But i got error like this

{
"message": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M",
"code": "ValidationException",
"time": "2016-10-26T11:04:60.756",
"requestId": "SN0NPRHDFHUKBBJHOVI0DFHHRQNSO5AEMVJFFGHF9ASFHUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}


i need response like this after updation

column1 : email

column2 : details


manaf1@gmail.com |
[{"id":"1","mob":"978956"},{"id":"2","mob":"767886"}]

manaf2@gmail.com |


what is the issue related with my code?

Answer

When the details attribute is NOT present in the item, the update expression should have just SET without the list_append.

UpdateExpression : "SET #details = :details"

When the details attribute is present in the item and the new update needs to add additional elements in the list, then list_append can be used to add the new element to the list.

UpdateExpression : "set #details = list_append (#details, :details)"