0
votes

I've been stuck on this code for about a day now.

I am just trying to add information into DynamoDB through a launch request using Alexa.

I get the following error code:

"errorMessage": "RequestId: f96ae2cb-1dbf-11e7-a267-b7cf2f2c95a0 Process exited before completing request"

The information actually gets inserted into DynamoDB, but I can't add more functions to the program because of the error.

From what I understand, it may be a problem with the callback.

I have tried many different ways to "callback" or return something, but I haven't figured out how to avoid the error.

If I uncomment this.emit(':tell', "Hello, inserting Apples into DynamoDB"); the error goes away, but no information gets inserted.

What am I doing wrong and how can I fix it?

Below is my code;

'use strict';
var Alexa = require('alexa-sdk');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();

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

var handlers = {
  'LaunchRequest': function(event, context, callback) {
    // this.emit(':tell', "Hello, inserting Apples into DynamoDB");
    var params = {
      Item: {
        date: Date.now(),
        message: "Apples"
      },
      TableName: '_yourTableName'
    };
    dynamo.putItem(params, function(err, data) {
      if (err) {
        callback(err, null);
      } else {
        callback(null, data);
      }
    });
    context.done();
  }
};
1
Your Lambda function is probably exiting before the PutItem request has completed. You need to keep your function running until your task is done, then call context.done(). See: Everything Depends on Context or, The Fine Art of nodejs Coding in AWS Lambda - John Rotenstein
Thanks for the response. I checked out the link and added context.done(). I now get the error "errorMessage": "Cannot read property 'done' of undefined" when I put the statement outside of the dynamo.putItem function. If I put context.done() directly after the else statement, I get the same error. - Michael
Sorry, I don't know any more detail on the context -- it's just that many people (myself included) have run into that problem so I wanted to mention it. - John Rotenstein
No worries, thanks for the bit of advice! - Michael
have you found a solution? - Kevin Wu

1 Answers

0
votes

This is because you are adding values to Dynamodb, which is a callback but context.done(); is written outside the callback. Before dynamoDb completes the operation it will call context.done(); hence it will exit the process