Leonardo
Leonardo

Reputation: 95

DynamoDB: Appending an element to a list using Node.js

This doesn't work. Is there another way to do this? cord is a list to which I want to add a map.

var params5 = {
  TableName: 'rides',
  Key: {
    'rid': data2.Items[0].rid
  },
  UpdateExpression: 'add cord :x',
  ExpressionAttributeValues: {
    ':x': [{date: secondStartDate.toString(), latitude: xcorpassed, longitude: ycorpassed}]    
  },
  ReturnValues: 'UPDATED_NEW'
}
docClient.update(params5, function (err5, data5) { ... }

Upvotes: 3

Views: 5258

Answers (2)

Khalid T.
Khalid T.

Reputation: 10567

Instead of ADD, you could use SET with the list_append function (in general, AWS recommends using SET rather than ADD):

(NOTE: The list_append function name is case-sensitive)

var params = {
  TableName: "rides",
  Key: {
    "rid": data2.Items[0].rid
  },
  UpdateExpression: "SET #c = list_append(#c, :vals)",
  ExpressionAttributeNames: {
     "#c": "cord"
  },
  ExpressionAttributeValues: {
    ":vals": [{date: secondStartDate.toString(), latitude: xcorpassed, longitude: ycorpassed}]    
  },
  ReturnValues: "UPDATED_NEW"
}

docClient.update(params, function (err, data) {
   if (err) console.log(err);
   else console.log(data);
}

Upvotes: 7

Max Phillips
Max Phillips

Reputation: 7499

Without seeing the error code it throws it looks like you should change add to set and don't forget the = sign.

 var params5 = {
      TableName: 'rides',
      Key: {
        'rid': data2.Items[0].rid
      },
      UpdateExpression: 'set cord = :x',
      ExpressionAttributeValues: {
        ':x': [{date: secondStartDate.toString(), latitude: xcorpassed, longitude: ycorpassed}]    
      },
      ReturnValues: 'UPDATED_NEW'
    }
    docClient.update(params5, function (err5, data5) {

Upvotes: 0

Related Questions