How to callback an error message when searching a table is unsuccessful in DynamoDB

0 votes

I'm currently using AWS Lambda JavaScript code to try and search a DynamoDB table this is then implemented into an Amazon Alexa application, but that isn't really important for what I'm asking. Here is the code I'm struggling with:

function readDynamoItem(params2, callback) {
        var AWS = require('aws-sdk');
        AWS.config.update({region: AWSregion});

        var dynamodb = new AWS.DynamoDB();
        console.log('reading item from DynamoDB table');

        dynamodb.scan(params2, function (err, data){
            if (err) {
                callback("error");
                //console.log(err, err.stack); // an error occurred
            }
            else{
               callback(data);
            }
        });
    }

So when an error occurs I want it to callback the message "error" and then use it here:

const params2 = {
            TableName: 'Fixtures',
            FilterExpression: 'team1 = :value',
            ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
};

readDynamoItem(params2, myResult=>{
            say = myResult;
            this.response.speak(say).listen('try again');
            this.emit(':responseReady');
});

All I'm getting at the moment is this response when I test, I think due to err just ending the program instead of calling the error back to use in the implementation:

Response:
{
  "errorMessage": "RequestId: 0f586880-2ddb-11e8-bdf7-07b4c224b25d Process exited before completing request"
}

Any help would be greatly appreciated.

Here's the full code for my project for further reference:

const AWSregion = 'eu-west-1';  
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');

AWS.config.update({
    region: AWSregion
});

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);

    // alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
    // alexa.dynamoDBTableName = 'YourTableName'; // creates new table for session.attributes

    alexa.registerHandlers(handlers);
    alexa.execute();
};

const handlers = {
    'LaunchRequest': function () {
        this.response.speak('welcome to magic answers.  ask me a yes or no question.').listen('try again');
        this.emit(':responseReady');
    },


    'MyIntent': function () {
        var MyQuestion = this.event.request.intent.slots.MyQuestion.value;
        console.log('MyQuestion : ' + MyQuestion);


        const params2 = {
            TableName: 'Fixtures',
            FilterExpression: 'team1 = :value',
            ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
        };
        const params3 = {
            TableName: 'Fixtures',
            FilterExpression: 'team2 = :value',
            ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
        };

        readDynamoItem(params2, myResult=>{
            var say = MyQuestion;
            //if nothing is found when scanning for team1, scan team2
            if (myResult == "error"){
                readDynamoItem(params3, myResult2=>{
                    say = myResult2;
                    say = 'The top scorer for ' + MyQuestion + ' is ' + myResult2;
                    this.response.speak(say).listen('try again');
                    this.emit(':responseReady');
                });
            } 
            else{
                say = myResult;
                say = 'The top scorer for ' + MyQuestion + ' is ' + myResult;
                this.response.speak(say).listen('try again');
                this.emit(':responseReady');
            }
        });

    },
    'AMAZON.HelpIntent': function () {
        this.response.speak('ask me a yes or no question.').listen('try again');
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak('Goodbye!');
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak('Goodbye!');
        this.emit(':responseReady');
    }
};

Edit: I have tried testing this code with .query instead of .scan and the error callback works perfectly which is strange but obviously for this implementation I need to use .scan

Sep 11, 2018 in AWS by bug_seeker
• 15,520 points
1,332 views

1 answer to this question.

0 votes

When you get the "Process exited" response from the Lambda it is helpful to log heavily to see where the Lambda is getting stuck and then check the Cloudwatch Logs to get to the detail.

Then you can pinpoint the exception and focus on it. At least for me, the root cause was many times unexpected as Lambdas force a different way of thinking.

answered Sep 11, 2018 by Priyaj
• 58,100 points

Related Questions In AWS

0 votes
2 answers

How to skip headers when reading a CSV file in S3 and creating a table in AWS Athena?

Thanks for the answer. This should be clear ...READ MORE

answered Aug 14, 2019 in AWS by athenauserz
11,788 views
0 votes
1 answer

How to load data into a table in DynamoDB using Java?

You can create a .json file with the ...READ MORE

answered Feb 26, 2019 in AWS by Priyaj
• 58,100 points
6,912 views
0 votes
1 answer

In Amazon Data Pipeline, how to make sure only once instance of a pipeline is running at any time?

On the CopyTablesActivity, you could set a lateAfterTimeout attribute ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,100 points
2,127 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to create a DynamoDB table in AWS?

Creating a DynamoDB table is made very ...READ MORE

answered Feb 22, 2019 in AWS by Priyaj
• 58,100 points
1,196 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP