Reputation: 289
I am using the api "updateItem" in AWS dynamodb for update the items. The main feature will be add the new item or delete one existed item. However, none of them works in the following code, the error message for the ADD is " "ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: S. Here is the my code:
const data = "newData";
async function updatedb(data) {
return new Promise((resolve, reject) =>{
var params = {
Key: {
"lists": {
S: data
}
},
TableName: "old_lists_DB",
UpdateExpression: "SET #L = list_append(:v,#L) ",
ExpressionAttributeNames: {
"#L": "lists",
},
ExpressionAttributeValues: {
":v": {
S: data
}
}
};
dynamodb.updateItem(params, function(err,data) {
if(err){
reject(err);
}else{
resolve(data);
}
});
});
}
The schema of the old_lists_DB is very simple, only has one attribute : lists, and the value is a set of string. For example:
{
{"lists" : "a"},
{"lists" : "b"},
{"lists" : "c"},
}
Upvotes: 2
Views: 1501
Reputation: 10333
considering list can't be a partition key, you have a different partition key and list is just an attribute. So, you have a string data
, that you want to append to lists
of type list.
with this, I see two things,
Key: This should contain partition key information, which in this case I assumed id
of type string.
list_append, we should pass a list itself. so, ExpressionAttributeValues
we need to frame an array with a string.
Here is modified update code.
const data = "newData2";
const id = "1";
var params = {
Key: {
id: {
S: id,
},
},
TableName: "old_lists_DB",
UpdateExpression: "SET #L = list_append(:v,#L) ",
ExpressionAttributeNames: {
"#L": "lists",
},
ExpressionAttributeValues: {
":v": {
L: [{ S: data }],
},
},
};
dynamodb.updateItem(params, function (err, result) {
if (err) {
console.log("err", err);
} else {
console.log("result", result);
}
});
Upvotes: 0