node.js:DynamoDB DocumentClient 返回空对象

标签 node.js amazon-dynamodb dynamo-local

我在本地使用 DynamoDB,可以创建和删除表。我创建了一个只有一个键的表,如下所示

const tablePromise = dynamodb.listTables({})
    .promise()
    .then((data) => {
        const exists = data.TableNames
            .filter(name => {
                return name === tableNameLogin;
            })
            .length > 0;
        if (exists) {
            return Promise.resolve();
        }
        else {
            const params = {
                TableName: tableNameLogin,
                KeySchema: [
                    { AttributeName: "email", KeyType: "HASH"},  //Partition key

                ],
                AttributeDefinitions: [
                    { AttributeName: "email", AttributeType: "S" },
                ],
                ProvisionedThroughput: {
                    ReadCapacityUnits: 10,
                    WriteCapacityUnits: 10
                }
            };
            dynamodb.createTable(params, function(err, data){
              if (err) {
                console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
              } else {
                console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
              }
            });
        }
    });

现在我想在下面的例子doc 的表中插入一个项目在AWS。

var docClient = new AWS.DynamoDB.DocumentClient();
var tableNameLogin = "Login"
var emailLogin = "abc@gmail.com";

var params = {
    TableName:tableNameLogin,
    Item:{
        "email": emailLogin,
        "info":{
            "password": "08083928"
        }
    }
};

docClient.put(params, function(err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));

    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});

当我运行插入项目代码时,我得到 Added item:{} 为什么输出一个空对象?它实际上插入了什么吗?我调查了this callback example但是这次它没有输出任何东西。

最佳答案

您需要将 ReturnValues: 'ALL_OLD' 添加到您的 put 参数中。它看起来像下面提到的那样。

var params = {
    TableName:tableNameLogin,
    Item:{
        "email": emailLogin,
        "info":{
            "password": "08083928"
        }
    },
    ReturnValues: 'ALL_OLD'
};

更多详情可以关注这个https://github.com/aws/aws-sdk-js/issues/803

关于node.js:DynamoDB DocumentClient 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45622651/

相关文章:

javascript - 如何为 passportjs 使用 jquery/ajax 数据

node.js - 使用nodejs中的另一个模型数据填充表单中的下拉列表

java - 如何在java中为内存DynamoDB本地设置aws.access.key和aws.secret.key?

javascript - 我是否必须使用 "this."才能在 NodeJs 中使用 "object"属性?

node.js - Kubernetes Nginx Ingress 和 Socket.io 连接问题

python - dynamodb条件更新项(客户端)

perl - 在 EC2 上扩展 MongoDB 还是应该切换到 DynamoDB?

java - org/apache/http/util/Args (java.lang.NoClassDefFoundError)。消息负载的类型为 : String

amazon-dynamodb - 错误 InvalidParameterType : Expected params. 项目 ['pid'] 成为 DynamoDB 中的结构