How to append an item to an array in DynamoDB

Viewed 55

Hi im trying to append an item to a dynamoDB array.

exports.handler = (event,context,callback)=>{
let params = {
    Key:{
        "userName":event.userName,
    },
    UpdateExpression:"set #FutureTrips = list_append(if_not_exists(#FutureTrips,empty_list),country)",
    ExpressionAttributeNames:{
        "#FutureTrips" : "FutureTrips"
    },
    ExpressionAttributeValues: {
        ':country':["Japan"],
        ":empty_list":[]
    },
    TableName:"Users"
    };


dynamoDB.updateItem(params,function(err,data){
    if(err){
        console.log(err);
        callback(null,err);
    }
    else{
        callback(null,data);
    }
});

};

it returns me "message": "Unexpected key '0' found in params.ExpressionAttributeValues[':country']",

im trying to append an item to an array called FutureTrips thanks for the help

1 Answers

Based on the code you share, currently the UpdateExpression is not referring to the specified ExpressionAttributeValues.

UpdateExpression:"set #FutureTrips = list_append(if_not_exists(#FutureTrips,empty_list),country)"

Correct UpdateExpression should be;

UpdateExpression: "set #FutureTrips = list_append(if_not_exists(#FutureTrips, :empty_list), :country)"
Related