Search code examples
javascriptnode.jstypescriptamazon-web-servicesamazon-dynamodb

The document path provided in the update expression is invalid for update


 const updateExpression =
      "SET #received.#type = list_append(if_not_exists(#received.#type, :emptyList), :newHomework)";

    // Define attribute names and values
    const expressionAttributeNames = {
      "#received": "received",
      "#type": "books",
    };

    const expressionAttributeValues = {
      ":newHomework": { L: [{ M: { homework: { S: assignerId } } }] },
      ":emptyList": { L: [] }, // This is an empty list in DynamoDB
    };

    // Create UpdateItem command input
    const params: UpdateItemCommandInput = {
      TableName: this.tableName,
      Key: {
        userId: { S: userId.toString() },
      },
      UpdateExpression: updateExpression,
      ExpressionAttributeNames: expressionAttributeNames,
      ExpressionAttributeValues: expressionAttributeValues,
      ReturnValues: "UPDATED_NEW",
    };
    this.logger.debug("Update params", params);
    await this.instance.send(new UpdateItemCommand(params));

I'm trying to append an object to the list but I'm getting that weird issue. I should have something like received:{books:[objects]}. What I am doing wrong? Thanks!


Solution

  • The reason you receive this error is because the map received does not exist. So when you use #received.#type, as #received does not exist it throws a ValidationException.

    A simple way to overcome it is to create the received map when the item is created:

    {
     "userId": "someId",
     "received": {},
     ...
    }