How to append new value to existing list in AWS DynamoDB on typescript

I'm trying to append a new string into my "savedPosts" array and my first attempt was to read the previous data, then append the new element and finally update. Although after checking documentation, it says that I can skip the reading step and update my existing list with one call.

My code so far:

await UserModel.update({
  awsCognitoId: req.user,
  savedPosts: {$add: postId}
});

Error:

{
    "code": "ValidationException",
    "message": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: STRING, typeSet: ALLOWED_FOR_ADD_OPERAND"
}

Upvotes: 0

Views: 1840

Answers (1)

Ronique Ricketts
Ronique Ricketts

Reputation: 290

I had the same issue and what I did to fix this was change my UpdateExpression from an ADD to a SET. I also used the list_append() to accomplish this. Please ensure you're appending 2 Arrays.

Please take a look at this url https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html Jump to Appending Elements to a List

Upvotes: 1

Related Questions