简体   繁体   中英

Why is dynamodb.update() is not updating the field in my Table?

I have a dynamodb Table named "user_subscription", I want to update the status field from "ACTIVE" to "INACTIVE",

 const params = {
    TableName: "user_subscription",
    Key: {
      userId: userId,
      subscription_id: "abc",
    },
    updateExpression: "SET status = :newStatus",
    expressionAttributeValues: { ":newStatus": "INACTIVE" },
    ReturnValues: "ALL_NEW",
  };
  console.log("params", params);
  try {
    dynamoDb.update(params, (error, data) => {
      console.log("error", error, "data", data);
    });
  } catch (err) {}

This code doesn't update the status field.

The response data which I get is:

Attributes: {
    plan_id: 'dkdkkd',
    subscription_id: 'abc',
    userId: 'ebef4c92-9fa8-4e1b-878f-d5753bb4042a',
    updatedAt: '2022-12-25T08:18:32.681Z',
    status: 'ACTIVE',
    createdAt: '2022-12-25T08:18:32.681Z'
  }

Status is a reserved keyword in DynamoDB, I believe the code you shared should be throwing several errors, are you sure you are executing the correct code while testing?

To fix your errors, try these params:

const params = {
    TableName: "user_subscription",
    Key: {
      userId: userId,
      subscription_id: "abc",
    },
    UpdateExpression: "SET #status = :newStatus",
    ExpressionAttributeValues: { ":newStatus": "INACTIVE" },
    ExpressionAttributeNamee: { "#status": "status" },
    ReturnValues: "ALL_NEW",
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM