2017-01-20 182 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

相關問題