0
votes

I can't figure out why I don't see any response of the DynamoDB in my Alexa Skill.

I created several variations of the promise with DynamoDB from different sources to figure it out. But nothing seems to work. There is not even one error of the DynamoDB in the logs. This drives me crazy.

I tried scan, get and put operations.

Here the code of my scan try in AWS lambda:

....

'StartIntent': function () {
    speechOutput = '';

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

    var docClient = new AWS.DynamoDB.DocumentClient();

    var params = {
        TableName: "my_db_2",
        FilterExpression: "data_type = card"
    };

    console.log("Scanning table.");

    docClient.scan(params, onScan).promise().then(function(result)  {
        console.log("GetItem succeeded:", JSON.stringify(result, null, 2));
        console.log("succeeded");
        }).catch(error => {
    console.log("ERROR catched");
    });
    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 {
        console.log("Scan succeeded.");
        data.Items.forEach(function(movie) {
           console.log(movie.data_type);
        });
    }
}

Cloudwatch shows only:

2018-09-13T11:43:11.500Z Scanning table.

2018-09-13T11:43:11.522Z scan done

2018-09-13T11:43:11.659Z scan of 2nd done

1

1 Answers

0
votes

The scan is not synchronous. You are using a promise to encapsulate the asynchronous behavior. Basically console.log("Scanning table.") and console.log("scan done") will get called immediately because they are part of the same synchronous block. Then when the scan operation finishes, console.log("succeeded") will be called. Since you don't appear to be waiting for the scan to finish, I assume your lambda is exiting before you ever get your data back from DynamoDB.