dynamodb nodejs put vs update code example
Example: set in Dynamo DB not properly incrementing nodejs lamnda function
var AWS = require('aws-sdk');
var documentClient = new AWS.DynamoDB.DocumentClient({'region': 'eu-central-1'});
exports.handler = function(item, context, callback) {
var params = {
TableName: "my-table",
Key:{
"id": item.id
},
UpdateExpression: "ADD iteration :iteration SET itemdata = :itemdata",
ExpressionAttributeValues:{
':iteration': 1,
':itemdata' : item.data
},
ReturnValues:"NONE"
};
documentClient.update(params, function(err, data) {
if (err) {
console.log("Error", err);
const errResponse = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({ Error: 500, device : "DynamoDB", detail : err })
};
callback(null, errResponse);
} else {
console.log("Success", params.Items);
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify("upsert complete.")
};
callback(null, response);
}
});
};