0

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

enter image description here

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();
  }
});

3 Answers 3

1

list_append takes two arguments, not one: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

Sign up to request clarification or add additional context in comments.

Comments

1

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
  };

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.