DynamoDB: Value provided in ExpressionAttributeNames unused in expressions: keys: {#date}

Viewed 16316

I am trying to update an item by changing value of isRelevant to true:

       var params = {
              TableName: "test",
              Key: {
                  "#date": data.Items[i].date.N,
                  "accountid": data.Items[i].accountid.S
              },
              UpdateExpression: "set #uu = :x",
              ExpressionAttributeValues: {
                  ":x": {"BOOL": false}
              },
              ExpressionAttributeNames: {
                   '#uu': "isRelevant",
                   '#date': "date"
              }
          };
          
          docClient.update(params, function(err, data) {
              if (err) console.log(err);
              else {
                  console.log('worked');
              }
          });

What is wrong in this code? I tried all possible ways, but still not working!

2 Answers

You don't need to use attribute name mapping for your keys in dynamo.

At the moment you params read like you have a key called '#date' and that you've randomly declared an attribute called '#date' that you're not using.

Instead try:

var params = {
  TableName: "test",
  Key: { "date": data.Items[i].date, "accountid": data.Items[i].accountid },
  UpdateExpression: "set #uu = :x",
  ExpressionAttributeValues: { ":x": false },
  ExpressionAttributeNames: { '#uu': "isRelevant" }
};

Also- when using DynamoDB.DocumentClient, you should use JSON values and it'll deal with the marshalling info the dynamo typed format.

If your date id not Key in dynamoDB than you have to use in ConditionExpression

Example :

let queryParams = {
      TableName: "test",
      Key: {
        'accountid': data.Items[i].accountid
      },
      UpdateExpression: "set #uu = :x",
      ConditionExpression: 'date = :date',
      ExpressionAttributeValues: {
        ':x': false,
        ':date': data.Items[i].date,
      },
      ExpressionAttributeNames: {
        '#uu': "isRelevant"
   }
};
Related