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

时间:2013-09-29 03:07:54

标签: node.js amazon-web-services amazon-dynamodb

人们,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);
  });
 }

输出:

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

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

3 个答案:

答案 0 :(得分:18)

必须遵循SDK和文档,其简单: 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();
  });

答案 1 :(得分:2)

我试图按照文档中的建议进行操作,但是也出现错误。

最后完成了以下工作:

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();

}

答案 2 :(得分:1)

以下是使用NodeJS的DynamoDB的优秀资源: