skip to Main Content

I am creating a Node.js application where some Lambda functions mutate data atomically called via API.
Creation function run perfectly, but update function, with the same structure, gives "Unable to update Member data" (where Member is the table to update), but the error messagge is empty.
The piece of code is:

    const ddb = new DynamoDBClient({ region: 'eu-central-1' });

    const { MemberId, ...updateFields } = JSON.parse(event.body);
    let updateExpression = "SET ";
    const expressionAttributeValues = {};
    Object.keys(updateFields).forEach((key, index) => {
        updateExpression += `${key} = :val${index}, `;
        expressionAttributeValues[`:val${index}`] = updateFields[key];
    });
    updateExpression = updateExpression.slice(0, -2);

    const params = {
        TableName: "Members",
        Key: {
            //MemberId: { N: MemberId.toString() }
            MemberId: { N: MemberId }
        },
        UpdateExpression: updateExpression,
        ExpressionAttributeValues: expressionAttributeValues
    };
    console.log('params:', JSON.stringify(params));

    try {
        const data = await ddb.send(new UpdateItemCommand(params));
        console.log('UpdateItemCommand response:', data);
        if (!data.Attributes) {
            responseBody = 'Member not found';
            statusCode = 404;
        } else {
            responseBody = 'Member successfully updated';
            statusCode = 200;
        }
    } catch (err) {
        responseBody = 'Unable to update Member data: ' + JSON.stringify(err);
        statusCode = 403;
    }

The response is:

{
  "headers": {
    "Content-type": "application/json"
  },
  "statusCode": 403,
  "body": "Unable to update Member data: {}"
}

Function Logs
START RequestId: c4ddd7c2-4ba8-4c32-b0c0-552e823befce Version: $LATEST
2024-04-08T08:54:54.083Z    c4ddd7c2-4ba8-4c32-b0c0-552e823befce    INFO    params: {"TableName":"Members","Key":{"MemberId":{"N":"7807077375424"}},"UpdateExpression":"SET FirstName = :val0, AddressCity = :val1","ExpressionAttributeValues":{":val0":"James G.",":val1":"Dundee, UK"}}
END RequestId: c4ddd7c2-4ba8-4c32-b0c0-552e823befce
REPORT RequestId: c4ddd7c2-4ba8-4c32-b0c0-552e823befce  Duration: 244.31 ms Billed Duration: 245 ms Memory Size: 128 MB Max Memory Used: 83 MB  Init Duration: 329.88 ms

The variable params is correctly assigned and the key value of MemberId exists in the table.

I have added the variable data to log the response of the ddb.send, but the error occurs on the ddb.send itself and err is empty. Conversion of MemberId into a string does not change the result.

How can I detect what happens in ddb.send and solve the issue? Thanks a lot!

2

Answers


  1. As you are using the low-level client you must also define your ExpressionAttributeValues using DynamoDB JSON… for example:

    {
        ":c": { "S": "Black" },
        ":p": { "N": "500" }
    }
    
    Login or Signup to reply.
  2. I believe you should also extend the JSON object of the expressionAttributes Value with the data type as mentioned in the doc https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeValues.html

    Also is see a 403, wondering if you have the right access defined in the IAM policy for adding items to dynamodb (https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazondynamodb.html)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search