【问题标题】:Amazon Web Services: Delete item on DynamoDB Database from Lambda FunctionAmazon Web Services:从 Lambda 函数中删除 DynamoDB 数据库中的项目
【发布时间】:2015-11-03 02:01:03
【问题描述】:

如何通过 Lambda 函数从 DynamoDB 数据库中删除项目。我知道如何放置和项目。这是工作代码:

dynamo.putItem({
            "TableName": "Table",
            "Item": item
        }, function(err, data) {
            if (err) {
                console.log("Failure: " + err);
                context.succeed("Failure!");
                context.done();
            } else {
                console.log("Success!");
                context.succeed("Success!");
                context.done();
            }
        });

【问题讨论】:

    标签: node.js database amazon-web-services aws-lambda


    【解决方案1】:

    这对我有用...省略了 lambda 样板文件...

        var tableName = "Users"
    dynamodb.deleteItem({
        "TableName": tableName, 
        "Key" : {
            "UserId": event.UserId
        }
    }, function (err, data) {
        if (err) {
            context.fail('FAIL:  Error deleting item from dynamodb - ' + err);
        }
        else {
            console.log("DEBUG:  deleteItem worked. ");
            context.succeed(data);
        }
    });
    

    【讨论】:

      【解决方案2】:

      更正上面的代码,这是有效的:

      var tableName = "Users";
      dynamodb.deleteItem({
          "TableName": tableName, 
          "Key" : {
              "UserId": {
                  "N" : event.userId.toString()
               }
          }
      }, function (err, data) {
          if (err) {
              context.fail('FAIL:  Error deleting item from dynamodb - ' + err);
          } else {
              console.log("DEBUG:  deleteItem worked. ");
              context.succeed(data);
          }
      });
      

      【讨论】:

        【解决方案3】:
        const AWS = require('aws-sdk');
        const ddb = new AWS.DynamoDB.DocumentClient();
        
        exports.handler = (event, context, callback) => {
          const connectionId = event.requestContext.connectionId;
          deleteConnectionId(connectionId).then(() => {
            callback(null, { statusCode: 200 , message: 'userId deleted'});
          });
        };
        
        
        function deleteConnectionId(connectionId) {
          return ddb
            .delete({ TableName: 'your table name', 
                Key: {
                    userId : connectionId.toString() // userId is my PK in this case
                 }
            } )
            .promise();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          • 1970-01-01
          • 2016-11-05
          • 1970-01-01
          • 2021-10-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多