smith felk
smith felk

Reputation: 49

How to delete an element from a DynamoDB List in AWS Lambda?

here is the image of data in dynamodb

i want to delete the element from the favRest and i will have to give the value only and it sould do that here is my lambda function

var AWS = require('aws-sdk');

const docClient = new AWS.DynamoDB.DocumentClient({ region: 'us-east-1' });

exports.handler = (event, context, callback) => {
    const params = {
        TableName : 'User',
        Key:{
            "id": event.user_id,
        },
        UpdateExpression: "DELETE favRest :p",
        ExpressionAttributeValues: {
            ':p': event.place_id
        },
        ReturnValues: "ALL_NEW" 
    }

    // TODO: Implementation...

    docClient.update(params, (err, data) => {
        if (err) {
            console.log("Unable to update item. Error: " + err.message);
            callback(err);
        } else {
            console.log("UpdateItem succeeded.");
            callback(null, data);
        }
    });
};

However, it's giving me the following error:

"{\n  \"message\": \"Invalid UpdateExpression: Incorrect operand type for operator or function; operator: DELETE, operand type: STRING\",\n  \"code\": \"ValidationException\",\n  \"time\": \"2018-04-29T18:49:58.628Z\",\n  \"requestId\": \"7TDRI4TOF9S71OUJDEKMIOA40RVV4KQNSO5AEMVJF66Q9ASUAAJG\",\n  \"statusCode\": 400,\n  \"retryable\": false,\n  \"retryDelay\": 35.34160854636804\n}"

what should i have to do

Upvotes: 2

Views: 2227

Answers (1)

Khalid T.
Khalid T.

Reputation: 10567

The DELETE action only supports Set data types. Your favRest attribute type is List.

If you want to keep your favRest attribute type as List, you could either use REMOVE to delete individual elements from the list (knowing the index of the element):

UpdateExpression: "REMOVE favRest[:index]"

or you could use SET to replace the whole list with new values:

UpdateExpression: "SET favRest = :newList"

Otherwise, you could change favRest attribute type to Set.

See Update Expressions.

Upvotes: 2

Related Questions