Reputation: 157
I'm trying to get an item from my DynamoDB database. The way my code is presently written, I fail to retrieve any data from DynamoDB. I must be doing something wrong, because as far as I can tell from my test, my callback is not being called.
I spent all day on this yesterday and have been tinkering with it unsuccessfully since I woke up this morning.
If anyone can provide insight into what I'm doing wrong here, I would be very grateful. Thanks to everyone in advance!
Final note: The timeout on the Lambda function itself is set to 5 minutes. So I don't think the Lambda function is timing out before the db query can return. When I run the function, it exits after only a moment.
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();
var response = null;
var test = false;
function getFromDB(callback) {
const params = {
TableName: process.env['DB_TABLE_NAME'] // evaluates to 'test-table',
Key: {
"id": {
S: postId // evaluates to a big string, pulling it in from an SNS message. Verified it with console.log(). It stores the expected value.
}
}
};
dynamodb.getItem(params, function(err, data) {
if (err) callback(data, true); // an error occurred
else callback(data, true); // successful response
});
}
getFromDB((data, isCalled) => {
response = data;
test = isCalled;
});
console.log(data); // evaluates to null
console.log(test); // evaluates to false
Upvotes: 3
Views: 3357
Reputation: 51
I Had faced similar issue. I removed async in the statement below to resolve :
exports.handler = async (event,context)
Upvotes: 5
Reputation: 1581
I think what's going on is Lambda calls the function, but it's not going to wait for the call back, so it thinks it is done and exits.
I think I had a similar problem and resolved it by using Bluebird and async/await.
I can provide a snippet from my code if you need it
Upvotes: 2
Reputation: 14809
Have you loaded the SDK? I can't see it in your code snippet
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
EDIT: Included region
Upvotes: 1