2016-07-26 69 views
0

我在調用帶回調的另一個函數的lambda函數中調用以下函數。在回調內部,響應對象似乎沒有得到正確處理,因爲它在alexa模擬器中給出響應無效的錯誤。函數看起來像lambda函數responde對象沒有處理

function handleFirstEventRequest(intent, session, response) { 
    var repromptText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?"; 

    var sessionAttributes = {}; 
    var cardContent = ""; 

    var cardTitle = "Events on "; 
    //response.tell("There is an issue here vik"); 
    getJsonEventsFromWikipedia(function (events) { 
     var speechText = ""; 
     sessionAttributes.text = events; 
     session.attributes = sessionAttributes; 
     if (events.length == 0) { 
      speechText = "There is a problem connecting to Wikipedia at this time. Please try again later."; 
      cardContent = speechText; 
      response.tell(speechText); 
     } else{ 
       console.log("vik::::::::::::: wikipedia response received"); 
       console.log("values are:" + events); 
      var speechOutput = { 
      speech: "hi how are you", 
      type: AlexaSkill.speechOutputType.PLAIN_TEXT 
     }; 
     var repromptOutput = { 
      speech: "hi how are you", 
      type: AlexaSkill.speechOutputType.PLAIN_TEXT 
     }; 
      console.log("before response"); 
      response.ask(speechOutput, repromptOutput); 
      console.log("after response"); 
     } 
    }); 
} 

但是,getJsonEventsFromWikipedia響應之外的工作完全正常。我不確定這裏出了什麼問題。 lambda功能日誌顯示沒有錯誤等

回答

0

賠率是您的迴應無效。當你在模擬器中調用它時,你會看到什麼JSON回來?你沒有在上面指出。當您調用它時,Lambda日誌中會出現什麼內容?你也沒有說明。你有沒有嘗試用SessionStart事件測試你的lambda函數?我認爲,如果你嘗試這些,你應該能夠在代碼或設置中找到錯誤。

0

沒有完整的上下文很難說清楚。但假設您以非常不尋常的方式使用alexa技能,最可能的原因是:

getJsonEventsFromWikipedia是異步的。您的函數在執行回調之前成功退出,並且Lambda回調函數由代碼中的某個其他路徑調用,或者在Lambda入口函數返回之前根本沒有調用。解決方案是等待您的電話getJsonEventsFromWikipedia作出響應,並且只有在確認處理完畢後才能退出該功能。