简体   繁体   English

代码在本地运行,但不适用于AWS Lambda

[英]Code works locally but not on AWS lambda

the following lamdba code works perfectly fine when testing locally using Alex-app-server but when published and tested on AWS Lambda, it gets within the else statement and prints the console.log('OUT PUBLISH') But it doesn't publish the 'lambda/channelnumber' nor does it send the correct response back to me or print 'IN PUBLISH' 以下lamdba代码在使用Alex-app-server在本地进行测试时可以很好地工作,但是在AWS Lambda上发布和测试时,它进入else语句并打印console.log('OUT PUBLISH')但它不会发布'lambda / channelnumber'也不发送正确的回复给我或打印'IN PUBLISH'

Any ideas why its just completing the bottom half of the else statement and not touching the publish function? 有什么想法为什么它只完成else语句的下半部分而不接触发布功能?

Code Snippet where I believe the problem lies 我认为问题所在的代码段

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;

    }
}

Full Code 完整代码

'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;

This is most likely because of the asynchronous nature of your code. 这很可能是由于代码的异步性质。

You haven't told us what thingShadows.publish() does, but it appears to take a callback function as its second argument. 您尚未告诉我们thingShadows.publish()功能,但它似乎将回调函数作为第二个参数。 Presumably this function will be called when publish() has finished doing whatever it does. 大概在publish()完成任何操作后将调用此函数。

When running locally I would imagine that the output you see is (in this order): 在本地运行时,我可以想象您看到的输出是(按此顺序):

out publish
in publish

Notice that out publish gets called before in publish . 请注意, out publish前被调用in publish This is because the publish method is asynchronous, so execution will continue as soon as it is called. 这是因为publish方法是异步的,因此执行将在调用后立即继续。 In your case, you are calling return immediately after calling publish , which probably means your lambda job is ending before it has a chance to log in publish . 在您的情况下,您是在调用publish之后立即调用return ,这可能意味着您的lambda作业在有机会登录in publish之前已结束。

You haven't provided enough information about the rest of your lambda code/setup to provide a full answer, but you need to make sure that you are waiting for your publish method to have finished before continuing. 您尚未提供有关其余lambda代码/设置的足够信息,无法提供完整的答案,但是您需要确保在等待发布方法完成之前,才能继续。 One way to achieve this is to use the callback object that is passed to your lambda handler: 一种实现方法是使用传递给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();
  });  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM