代码在本地工作但不在AWS lambda上

时间:2017-01-20 13:57:36

标签: javascript node.js lambda aws-lambda

以下lamdba代码在使用Alex-app-server进行本地测试时工作正常,但在AWS Lambda上发布和测试时,它会在else语句中输出并打印console.log('OUT PUBLISH')但它没有' t发布'lambda / channelnumber'也不会将正确的回复发送给我或打印'IN PUBLISH'

为什么它只是完成了else语句的下半部分并且没有触及发布函数的任何想法?

我认为存在问题的代码段

function (request, response) {
    var channelNumber = request.slot('CHANNELNUMBER');

    if (_.isEmpty(channelNumber)) {
        var prompt = 'I didn\'t hear a channel code. Tell me a channel code.';
        response.say(prompt).shouldEndSession(true);
        return true;
    } else {

        //Doesn't publish any of this?????
        thingShadows.publish('lambda/channelNumber', channelNumber, function () {
            var prompt1 = 'Okay.';
            response.say(prompt1).shouldEndSession(true);
            console.log('in publish');
        });

     ////But prints this??
        console.log('out publish');
        return true;

    }
}

完整代码

'use strict';
module.change_code = 1;
var Alexa = require('alexa-app');
var skill = new Alexa.app('smartmote');
var awsIot = require('aws-iot-device-sdk');
var deviceName = "tv";
var _ = require('lodash');
var path = require('path');

var host = "XXXXXXXXXXXXXXXXXXXX.iot.us-east-1.amazonaws.com";

//App id is the skill being used.
var app_id = "amzn1.ask.skill.YYYYYYYYYYYYYYYYYYYYY";

var thingShadows = awsIot.thingShadow({

    keyPath: path.join(__dirname, '/Raspi.private.key'),
    certPath: path.join(__dirname, '/Raspi.cert.pem'),
    caPath: path.join(__dirname, '/root-CA.crt'),
    clientId: deviceName,
    region: "us-east-1",
});

var reprompt = 'I didn\'t hear a channel, tell me a channel number or name to change to that channel';

skill.launch(function (request, response) {
    var prompt = 'To change channel, tell me a channel number.';
    response.say(prompt).reprompt(reprompt).shouldEndSession(true);
});

skill.intent('ChannelNumberIntent', {
        'slots': {
            'CHANNELNUMBER': 'CHANNELID'
        },
        'utterances': ['{|Change|put} {|the|on} {|channel} {|to} {-|CHANNELNUMBER}']
    },
    function (request, response) {
        var channelNumber = request.slot('CHANNELNUMBER');

        if (_.isEmpty(channelNumber)) {
            var prompt = 'I didn\'t hear a channel code. Tell me a channel code.';
            response.say(prompt).shouldEndSession(true);
            return true;
        } else {

            thingShadows.publish('lambda/channelNumber', channelNumber, function () {
                console.log('in pub');
                var prompt1 = 'Okay.';
                response.say(prompt1).shouldEndSession(true);
                callback();
            });

            console.log('out pub');
            return true;

        }    
    }
);

module.exports = skill;

1 个答案:

答案 0 :(得分:3)

这很可能是因为代码的异步性质。

您还没有告诉我们thingShadows.publish()做了什么,但它似乎将回调函数作为其第二个参数。据推测,当publish()完成它所做的任何事情时,将调用此函数。

在本地运行时,我会想到你看到的输出是(按此顺序):

out publish
in publish

请注意out publish之前调用in publish。这是因为publish方法是异步的,因此一旦调用它就会继续执行。在您的情况下,您在调用return后立即调用publish,这可能意味着您的lambda作业在有机会记录in publish之前就已结束。

您尚未提供有关其余lambda代码/设置的足够信息以提供完整答案,但您需要确保在等待发布方法完成后再继续。实现此目的的一种方法是使用callback object that is passed to your lambda处理程序:

exports.myHandler = function(event, context, callback) {

  // Other code

  thingShadows.publish('lambda/channelNumber', channelNumber, function () {
    var prompt1 = 'Okay.';
    response.say(prompt1).shouldEndSession(true);
    console.log('in publish');

    // When the publish method is complete, we can call `callback`
    // to tell lambda we are done
    callback();
  });  
}