Kaguei Nakueka
Kaguei Nakueka

Reputation: 1108

Update DynamoDB table with Node.JS returns reserved key word error

I am trying to update a DynamoDB table by following this AWS Tutorial, but I getting the following error:

Unable to update item. Error JSON: {
  "message": "Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: hash",
  "code": "ValidationException",
  "time": "2021-11-10T07:54:18.832Z",
  "requestId": "1e657754-c33d-4a55-8245-a107765e4261",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 43.13890004813747
}

My code looks like this:

import AWS from 'aws-sdk';
var table = "Users";

export async function updateData(email, hash, callback){ 

    AWS.config.update({
        region: 'us-west-2',
        endpoint: 'http://localhost:8000'
      });

    var docClient = new AWS.DynamoDB.DocumentClient()

    var params = {
        TableName: table,
        Key:{
            "email": email
        },
        UpdateExpression: "set hash = :var",
        ExpressionAttributeValues:{
            ":var": hash
        },
        ReturnValues:"UPDATED_NEW"
    };

    docClient.update(params, function(err, data) {
        if (err) {
            console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
            callback(err, null);
        } else {
            callback(data, null);
        }
    });
}

updateData('[email protected]', 'hash', function(data, err){
    console.log(err, data);
})

Any table has the following fields:

As far as I can see, my code is very similar to the example. What am I missing there?

Upvotes: 1

Views: 349

Answers (1)

fedonev
fedonev

Reputation: 25669

hash is a dynamodb reserved keyword, so we must substitute it in as a ExpressionAttributeName.

    var params = {
        TableName: table,
        Key:{
            "email": email
        },
        UpdateExpression: "set #k = :var",
        ExpressionAttributeNames:{
            "#k": "hash"
        },
        ExpressionAttributeValues:{
            ":var": hash
        },
        ReturnValues:"UPDATED_NEW"
    };

Upvotes: 2

Related Questions