node.js dynamodb 节点 aws-sdk 简单的 getItem() 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19073991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:47:44  来源:igfitidea点击:

dynamodb node aws-sdk simple getItem() call

node.jsamazon-web-servicesamazon-dynamodb

提问by Cmag

Folks, New to Javascript... trying to do simple dynamo queries from node:

伙计们,Javascript 新手……尝试从节点执行简单的发电机查询:

var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();

var params = {
"TableName" : 'admins',
"Key" : [
      { "username" : { "S" : "foo" } },
    ],
}

 db.getItem(params, function(err, data) {
    console.log('error: '+ err);
    console.log(data);
    return next();
    res.send(data);
  });
 }

Output:

输出:

error: UnexpectedParameter: Unexpected key 'username' found in params.Key['0']

Thanks! Any help would be greatly appreciated!

谢谢!任何帮助将不胜感激!

采纳答案by Diana

I was trying to do it as it was suggested in the documentation, but also got errors.

我试图按照文档中的建议去做,但也出现了错误。

At the end the following worked:

最后,以下工作:

var aws = require('aws-sdk');
var db = new aws.DynamoDB({
  region: 'eu-central-1',
  maxRetries: 1
});

exports.handler = event => {
    return queryMyThings();
}

const queryMyThings = async (event) => { 
 var params = {
      Key: {
      "ColumnByWhichYouSearch": {
         S: "ValueThatYouAreQueriing"
        }
      }, 
      TableName: "YourTableName"
     };

    return await db.getItem(params).promise();

}

回答by Cmag

Must follow the SDK and Docs, its simple: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html

必须遵循 SDK 和 Docs,它很简单:http: //docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html

var params = {
    AttributesToGet: [
      "password"
    ],
    TableName : 'foo',
    Key : { 
      "username" : {
        "S" : "bar"
      }
    }
  }

  db.getItem(params, function(err, data) {
    if (err) {
      console.log(err); // an error occurred
      } 
    else {
      console.log(data); // successful response
      res.send(data);
      }
    return next();
  });

回答by Hanny

Here are great resources for DynamoDB using NodeJS:

以下是使用 NodeJS 的 DynamoDB 的重要资源: