Reputation: 2188
I am trying to append a message to an empty list in AWS DynamoDB.
Here is the error that I get when I run the function
Invalid UpdateExpression: Incorrect number of operands for operator or function; operator or function: list_append, number of operands: 1
Data structure in DynamodB
Below is the code:
var caseId = "1734009";
var chatMessage = { "2018-04-20T15:02:48Z":
{
"userId": "wQnUJrklzwWBDOsx83XVETSS7us2",
"message": "How are you"
}
}
var params = {
TableName : 'CHATS',
Key: {
"CASE_ID" : caseId
},
UpdateExpression : "SET CHAT_MESSAGES = list_append(:i)",
ExpressionAttributeValues : {
':i': [chatMessage],
},
ReturnValues:'UPDATED_NEW' // OTHER OPTIONS: NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW
};
documentClient.update(params, function(err, data) {
if(err) {
var message = "Chat message could not be saved, error:" + JSON.stringify(err, null, 2);
res.json({"status": "failure", "statusCode" : 400, "message": message});
} else {
next();
}
});
Upvotes: 0
Views: 4155
Reputation: 61
Look at the following example. Attribute 'graph' is created as empty list which can be later updated using list_append(). Please note I am using Boto3(python) to update my dynamodb table.
dynamoTable = dynamodb.Table('abc')
dynamoTable.put_item(
Item={
''<primary_key>'': <primary_key_value>,
'First Name': 'first_name',
'Last Name': 'last_name',
'password_hash': 'password_hash',
'salt': 'salt',
'graph': [],
}
Look at the following code to append element/message to the list :
dynamoTable = dynamodb.Table('abc')
dynamoTable.update_item(
Key={
'<primary_key>': '<primary_key_value>',
},
UpdateExpression="set #Graph = list_append(#Graph, :name)",
ExpressionAttributeNames={
'#Graph': 'graph',
},
ExpressionAttributeValues = {
':name': [# {'myObject':
{
"userId": "wQnUJrklzwWBDOsx83XVETSS7us2",
"message": "How are you"
}
#}
],
}
The code above will append a map in 'graph'
with the "userId" and "message"
Upvotes: 1
Reputation: 2188
By using the below params and referring to the AWS Docs provided by Jason Livesay, I was able to add items to an empty list.
var params = {
TableName : 'CHATS',
Key: {
"CASE_ID" : caseId
},
UpdateExpression : "SET #ri = list_append(#ri, :vals)",
ExpressionAttributeNames: {
"#ri": "CHAT_MESSAGES"
},
ExpressionAttributeValues : {
':vals': [chatMessage]
},
ReturnValues:'UPDATED_NEW' // OTHER OPTIONS: NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW
};
Upvotes: 1
Reputation: 6377
list_append takes two arguments, not one: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
Upvotes: 1