2017-08-10 89 views
0

我正在使用nodeJS代碼使用請求模塊進行其他調用。我也使用回調函數,但請求函數沒有得到執行。AWS Lambda無法將REST調用到外部API

我的流程轉到了searchTSTData函數,但請求方法沒有執行。

從回調函數我只得到responseString ='尚未使查詢休息',我已經在searchTSTData函數中初始化。它沒有根據API返回的響應進行更新,該響應應該是錯誤或成功響應字符串。

我已經將模塊包含在zip中,因爲lambda不會拋出錯誤並通過測試。 此外,我確信請求模塊不工作,因爲在Cloudwatch日誌中我沒有看到任何我寫在請求中的console.logs。

請提出我哪裏出了問題。我是NodeJS的新手。

這裏是代碼 -

'use strict'; 
const request = require('request'); 
const Alexa = require('alexa-sdk'); 
const APP_ID = 'amzn1.ask.skill.80a49cf5-254c-123a-a456-98745asd21456'; 

const languageStrings = { 
    'en': { 
     translation: { 
      TST: [ 
       'A year on Mercury is just 88 days long.', 
      ], 
      SKILL_NAME: 'TEST', 
      GET_TST_MESSAGE: "Here's your TST: You searched for ", 
      HELP_MESSAGE: 'You can say get me a TST, or, you can say exit... What can I help you with?', 
      HELP_REPROMPT: 'What can I help you with?', 
      STOP_MESSAGE: 'Goodbye!', 
     }, 
    }, 
}; 

const handlers = { 
    'LaunchRequest': function() { 
     this.emit('GetTST'); 
    }, 
    'GetNewTSTIntent': function() { 
     this.emit('GetTST'); 
    }, 
    'GetTST': function() { 
     // Get a random space fact from the space facts list 
     // Use this.t() to get corresponding language data 
     const inputValue = this.event.request.intent.slots.Search.value; 
     var finalResponse = "Some error occurred in code. Please try again later."; 
     console.log('Input recieved as '+inputValue); 

     searchTSTData(inputValue, function (response){ 
     console.log('trying to call'); 
         finalResponse = response;              
     }); 

     console.log("after function call"); 

     // Create speech output 
     const speechOutput = this.t('GET_TST_MESSAGE') + inputValue+". Here are the results " +finalResponse; 
     this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), speechOutput); 
    }, 
    'AMAZON.HelpIntent': function() { 
     const speechOutput = this.t('HELP_MESSAGE'); 
     const reprompt = this.t('HELP_MESSAGE'); 
     this.emit(':ask', speechOutput, reprompt); 
    }, 
    'AMAZON.CancelIntent': function() { 
     this.emit(':tell', this.t('STOP_MESSAGE')); 
    }, 
    'AMAZON.StopIntent': function() { 
     this.emit(':tell', this.t('STOP_MESSAGE')); 
    }, 
}; 

exports.handler = function (event, context) { 
    const alexa = Alexa.handler(event, context); 
    alexa.APP_ID = APP_ID; 
    // To enable string internationalization (i18n) features, set a resources object. 
    alexa.resources = languageStrings; 
    alexa.registerHandlers(handlers); 
    alexa.execute(); 
}; 


function searchTSTData(searchString,callback){ 
    var responseString = 'Yet to make query rest'; 

    request({ 
    url: 'https://api.google.com/getresultsInJson', 
    method: 'GET' 
    }, function (error, response, body) { 
      if (error) { 
       responseString = 'Error received from rest api. Please try again after some time.'; 
       } else if(response.statusCode===200){ 
       responseString = 'Sucess Success'; 
       }else{ 
       responseString = 'Nothing is working'; 
       } 
      }); 
      callback(responseString); 
      } 

回答

0

當你調用this.emit()時,Alexa-SDK結束你的lambda的事件循環。

在您的示例中,您正在調用異步工作的request()。在撥打request()(通過searchTSTData())之後,您立即發出this.emit(),這會結束事件循環並使響應未處理。

爲了處理你的迴應,你想呼叫保持到this.emit()

const handlers = { 
    'GetTST': function() { 
    searchTSTData(inputValue, (response) => { 
     const speechOutput = response; 
     this.emit(':tell', speechOutput); 
    }); 
    } 
}; 

這裏閱讀下的的NodeJS編程模型的lambda函數:http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html