2017-01-20 140 views
0

以下lamdba代码在使用Alex-app-server进行本地测试时工作得很好,但在AWS Lambda上发布和测试时,它会在else语句中获得并显示console.log(' OUT PUBLISH')但它不会发布'lambda/channelnumber',也不会将正确的响应发回给我或打印出'发布'代码适用于本地,但不适用于AWS lambda

任何想法为什么它只是完成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; 

回答

2

这是因为你的代码的异步性的可能性最大。

您还没有告诉我们thingShadows.publish()是做什么的,但它似乎采用回调函数作为其第二个参数。推测这个函数将在publish()完成任何操作时被调用。

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

out publish 
in publish 

注意out publishin publish之前调用。这是因为publish方法是异步的,所以一旦调用就会继续执行。在您的情况下,您在拨打publish后立即致电return,这可能意味着您的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(); 
    }); 
} 
+0

对不起,即时通讯新的JavaScript和node.js中作为一个整体,我不完全理解您的解决方案和回调一般。我已经更新了这个问题,以包含我的完整的lambda代码,希望你能进一步解释一下?谢谢 :) – Jacksilky

相关问题