Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

node.js - DynamoDB getItem call not giving a response

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...