Wildcard27
Wildcard27

Reputation: 1459

Have to test lambda several times before it works

I have a lambda function that I am playing around with. It inserts very basic information into a DynamoDB table. Here is the code:

'use strict';
const alexaSkillKit = require('alexa-skill-kit');
const AWS = require('aws-sdk');

function binDaySkill(event, context, callback) {
    alexaSkillKit(event, context, (message) => {

        let params = {
            Item:      {
                user_id:       '123',
                some_data: 'some data here'
            },
            TableName: 'my_table'
        };

        let documentClient = new AWS.DynamoDB.DocumentClient();

        documentClient.put(params, function (err, data) {
            if (err) {
                callback("Error", err);
            } else {
                callback(null, data);
            }
        });

    });
}

The issue I am having is that it only sometimes saves the data in the DB. I have to click test 5-10 times before it does anything.

Can anyone help with what might be causing this?

Upvotes: 0

Views: 44

Answers (1)

thomasmichaelwallace
thomasmichaelwallace

Reputation: 8482

The reason this is happening is because alexa-skill-kit takes care of the callback for you.

See the documentation. By passing in the context object you allow the wrapping handler (alexaSkillKit(...)) to manage decoding and encoding the returned objects and payload. The handler alexSkillKit callback function just expects you to return a value.

For your code sample you could do the following:

'use strict';
const alexaSkillKit = require('alexa-skill-kit');
const AWS = require('aws-sdk');

function binDaySkill(event, context, callback) {
  alexaSkillKit(event, context, (message) => {
    let params = {
      Item: {
        user_id: '123',
        some_data: 'some data here'
      },
      TableName: 'my_table'
    };

    let documentClient = new AWS.DynamoDB.DocumentClient();

    return documentClient.put(params).promise()
      .then((data) => {
        // stuff with the data!
        return data;
      }).catch((err) => {
        // stuff with the error
        throw err;
      });  
  });
}

n.b. The reason it worked after a few invocations is that lambda re-uses the environments each invocation executes in. It does this by effectively "freezing" the state of the environment and thawing it when it's needed again. This is the basis of a lot of optimisations people make; and it meant that you would sometimes thaw an environment that was midway through calling back when it was frozen by the alexaSkillKit returning first.

Upvotes: 2

Related Questions