-1

My updateItem params is like this:

{
  TableName: 'tablename',
  Key: {
    pk: {
      S: 'string'
    },
    sk: { S: '2022-06-13' }
  },
  UpdateExpression: 'ADD #views :amount',
  ExpressionAttributeNames: { '#views': 'views' },
  ExpressionAttributeValues: {
    ':amount': { N: '1' }
  }
}

Somehow, I got this error:

ValidationException: Invalid UpdateExpression: 
Incorrect operand type for operator or function; 
operator: ADD, operand type: MAP, typeSet: ALLOWED_FOR_ADD_OPERAND

It works when I pass directly the values ('value') instead of using AttributeValues ({S: 'value'}) in my param like this:

{
  TableName: 'tablename',
  Key: {
    pk: 'my-pk',
    sk: '2022-06-13' 
  },
  UpdateExpression: 'ADD #views :amount ',
  ExpressionAttributeNames: { '#views': 'views'},
  ExpressionAttributeValues: {
    ':amount': 1
  }
}

My code is like this:

try {
  const docClient = new AWS.DynamoDB.DocumentClient();
  await docClient.update(params).promise();
} catch (e) {
  console.error(e);
}

Maurice
  • 11,482
  • 2
  • 25
  • 45
phuwin
  • 3,130
  • 4
  • 26
  • 49
  • 1
    I don't understand - what's the problem? Sounds to me like it works. – Maurice Jun 13 '22 at 13:58
  • Read the [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#update-property) carefully. The behaviour you described is expected. – jellycsc Jun 13 '22 at 14:04
  • I want to use AttributeValue type but it doesn't work – phuwin Jun 14 '22 at 02:25
  • @jellycsc Alright, somehow from the link you shared, AttributeValue can't be used here. Strange because the param type (defined in the aws-sdk type package) for the `update` function requires the values to be AttributeValue. – phuwin Jun 14 '22 at 02:29

1 Answers1

0

I apparently used the wrong type for update's param. Here: https://stackoverflow.com/a/55169545/3588080

phuwin
  • 3,130
  • 4
  • 26
  • 49