1

I am just starting with AWS and can't get the DynamoDB running at all.

I followed a tutorial and created all AWS elements and set priviledge for the DynamoDB in the lambda basic profile.

I am wondering why I do not get any results from the DB or any error messages. I put some console logs in the code to troubleshoot:

var AWS = require("aws-sdk");
AWS.config.update({
    region: "eu-west-1",
});

var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
    TableName: "cooking_table",
    Key:{
        "data_type": "meal"
    }
};


console.log("Scanning table.");
docClient.scan(params, onScan);
console.log("scan done");

function onScan(err, data) {
    console.log("starting to scan");
    if (err) {
        console.error("Unable to scan the table. Error JSON:", 
 JSON.stringify(err, null, 2));
    } else {
        // print all the movies
        console.log("Scan succeeded.");
        data.Items.forEach(function(movie) {
           console.log(movie.data_type);
        });

        // continue scanning if we have more movies, because
        // scan can retrieve a maximum of 1MB of data
        if (typeof data.LastEvaluatedKey != "undefined") {
             console.log("Scanning for more...");
             params.ExclusiveStartKey = data.LastEvaluatedKey;
             docClient.scan(params, onScan);
        }
    }
}

Surprisingly, I do not get any console log entries inside of the function onScan.

In the log I see only the output of those lines:

console.log("Scanning table.");
console.log("scan done");

but no errors.

I don't see the big mistake I am doing.

What is going wrong? Thanks.

CKE
  • 1,533
  • 19
  • 18
  • 29
jack001
  • 13
  • 4

2 Answers2

0

"Key" isn't a valid param for scan. Not totally sure what your data looks like, but maybe you want:

var params = {
    TableName: "cooking_table",
    FilterExpression: "data_type = meal"
};

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property

Also, as noted in another answer, you want to do something like this to actually call the scan call, which returns a request object:

docClient.scan(params, onScan).send((err, data) => 
{
    console.log("scan actually done for real");
});
Brett
  • 56
  • 5
0

Scan is asynchronous action which call callback when completed so your problem is that your lambda is ending before that action is completed that's probably why you see only those 2 logs without any error/problems or result.

I would suggest to look on Promise as possible solution of that problem.

Jakub Bujny
  • 4,400
  • 13
  • 27