2

I'm having issues trying to access elements that are nested within a DynamoDB table using conditional expressions. The code I am trying is as follows:

const params = {
TableName: 'Games',
Key: {
  id: gameID.id,
},
UpdateExpression: 'SET #players = list_append(#answers , :answer)',
ConditionExpression: '#players CONTAINS :playerID',
ExpressionAttributeNames: {
  '#players': 'Players',
  '#answers': 'answers',
},
ExpressionAttributeValues: {
  ':playerID': userID.id,
  ':answer': tempAnswerList,
},
ReturnValues: 'ALL_NEW',

This version for example raises the error:

Invalid ConditionExpression: Syntax error; token: "CONTAINS", near: "#players CONTAINS :playerID"

Example DB Schema

Is there a way to look through an array without knowing it's id? Just as a note I have looked for wildcards to use on #players as it is a list but I cannot see one in the docs.

Thanks, Mark

1 Answer 1

2

Please change the contains as mentioned below and try.

ConditionExpression: 'contains (#players, :playerID)',
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for that suggestion but now I get Invalid ConditionExpression: Invalid function name; function: CONTAINS
function name is case sensitive. Please use lowercase. I have updated the answer.
Thanks again, I think I've made progress. I'm not getting the usual error but this time got The conditional request failed. So hopefully this is to do with my logic now rather than the request. Out of interest do you know of anyway to search through nested items without a [].?
Also, do you know how far contain searches through the object? As in will it look in any other child objects? I've gone over my CURL command and that should have returned one object so I'm a bit baffled.
Contains basically requires the entire object to match the item in the array (i.e. all the attributes of the specific item in the array). It requires answers, player and score (all attributes) to match the item in the array.
|

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.