📜  在 Dynamo DB 中设置未正确递增 nodejs lamnda 函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:44.028000             🧑  作者: Mango

代码示例1
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);
    }
  });
};