Bubble Trouble
Bubble Trouble

Reputation: 639

DynamoDB update using NODEjs: Syntax error token: "_", near: ", _geoloc"

Each object in my dynamoDB table looks something like

{
  _geoloc: {lat: 123, lng: 456},
  name: 'abc',
  city: 'belarus',
  id: 'unique1'
}

I have the following update expression:

const params = {
    TableName: CONFIG.dynamoDB.tableName,
    Key:{
      "id": location.id.toString()
    },
    UpdateExpression: "set city=:c, _geoloc=:g",
    ExpressionAttributeValues:{
      ":c": location.address.city,
      ":g": geoCodes
    },
    ReturnValues:"UPDATED_NEW"
  };

DynamoDB throws the following error:

ValidationException: Invalid UpdateExpression: Syntax error; token: "_", near: ", _geoloc"
    at Request.extractError (/Users/mv/pcode/meeting-finder-kinesis-consumer/node_modules/aws-sdk/lib/protocol/json.js:48:27)

As far as I know.. _ is a valid character to have in a name

any suggestions how I can fix this ?

Upvotes: 2

Views: 4940

Answers (2)

IanD
IanD

Reputation: 1

I was getting the same error as mentioned in question. In my case I was using the CLI. Took a while to figure out syntax so hopefully this is helpful.

I had a column with an underscore _ct which caused the ValidationException

The resulting update-item command was:

aws dynamodb update-item \
    --table-name myTable \
    --key '{ "pk": { "S": "myPrimarykey" },"sk": { "S": "mySortkey" } }' \
    --update-expression "SET #ct = :ct" \
    --expression-attribute-values file://values.json \
    --expression-attribute-names '{"#ct":"_ct"}'

values.json -->

{
    ":ct": {
      "S": "2022-12-07T11:00:10.881Z"
    }
  }

Upvotes: 0

notionquest
notionquest

Reputation: 39186

Agreed, the attribute name can contain underscore. However, in this case, you need to define a placeholder for attribute name in the update expression and use Expression Attribute Name to define the actual attribute name (as it contains special characters) for the placeholder.

"ExpressionAttributeNames" : "#geoloc = _geoloc"

Also, just replace the _geoloc on update expression with #geoloc.

This should resolve the issue.

Upvotes: 6

Related Questions