DynamoDB getItem call not giving a response

女生的网名这么多〃 提交于 2019-12-25 00:28:38

问题


I'm trying to read a basic DynamoDB table in AWS Lambda, following the AWS tutorials. I've got some basic code, that seems to be running OK (I'm not seeing any errors logged), but I can't get any output:

const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
const ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

function readData(){

console.log("In the readData() function");

var params = {
    TableName: "desks",
    Key: {
        "desk_id": {N:'1'}
    }
};
console.log("Set params");
// Call DynamoDB to read the item from the table
ddb.getItem(params, function(err, data) {
    console.log("In getItem callback function");
    if (err) {
        console.log("Error", err);
    }
    else {
        console.log("Success", data.Item);
    }
});
console.log("Completed call");
}

When my function above is called, the logs show the output "Set params" and "Completed call", but it's like the callback function doesn't get executed. Am I missing something around the execution flow?

Edit: I'm using Node.js 8.10 and I believe I've set up the appropriate role permissions (full access on the database).


回答1:


Ok, so I figured it out, Instead of working with specific API version, you can work with the DocumentClient.

About AWS.DynamoDB.DocumentClient :

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

About the get Function:

Returns a set of attributes for the item with the given primary key by delegating to AWS.DynamoDB.getItem().

Code snippet:

const AWS = require('aws-sdk');

const db = new AWS.DynamoDB.DocumentClient({
   region : 'REGION' 
});


function getItem(){

const params = {
  TableName : 'TABLE-NAME',
  Key: {
    'PRIMARY-KEY':'PRIMARY-KEY-VALUE'
  }
};

  db.get(params, (err, data) => {
  if (err){
    console.log("Error:", err);
  } 
  else{
    console.log("Success:", data.Item);

  } 
  console.log("Completed call");
});

}

exports.handler = (event) => {

 getItem()

};

By the way, you can use the Scan operation from DynamoDB UI,The scan operation returns one or more items that match the filter you specified.

I used it just to make sure the variables I entered are correct before hitting the lambda function.



来源:https://stackoverflow.com/questions/58156179/dynamodb-getitem-call-not-giving-a-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!