dynamodb node aws-sdk simple getItem() call

Problem

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

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!

Problem courtesy of: Cmag

Solution

Must follow the SDK and Docs, its simple: 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();
  });
Solution courtesy of: Cmag

Discussion

Here are great resources for DynamoDB using Node js:

Getting Started guide: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.html

Documentaion and examples: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

If you're using the DocumentClient:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

Discussion courtesy of: Hanny

This recipe can be found in it's original form on Stack Over Flow.