2017-08-10 2 views
0

Ich benutze den NodeJS-Code, um einen Restanruf mithilfe des Anforderungsmoduls auszuführen. Ich habe auch Callback-Funktion verwendet, aber die Anfrage-Funktion wird nicht ausgeführt.AWS Lambda kann REST-Aufruf an externe API nicht ausführen

Mein Fluss geht zur Funktion searchTSTData, aber die Anfrage-Methode wird nicht ausgeführt.

Von der Callback-Funktion bekomme ich nur responseString = 'Noch zu Abfrage ruhen', die ich in searchTSTData Funktion initialisiert habe. Es wird nicht basierend auf der Antwort aktualisiert, die von der API zurückgegeben wird, die entweder eine Fehler- oder eine Erfolgsantwort-Zeichenfolge sein sollte.

Ich habe die Module in zip enthalten, da Lambda keinen Wurffehler wirft und Test besteht. Auch bin ich sicher, dass Request-Modul funktioniert nicht wie in Cloudwatch-Protokolle ich sehe keine console.logs ich schrieb in Anfrage.

Bitte schlagen Sie vor, wo ich falsch gelaufen bin. Ich bin neu in NodeJS. Hier

ist der Code -

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

Antwort

0

Alexa-SDK beendet Ihre Lambda-Ereignisschleife, wenn Sie this.emit() aufrufen.

In Ihrem Beispiel rufen Sie request(), die asynchron arbeitet. Unmittelbar nach dem Aufruf request() (über searchTSTData()), senden Sie this.emit(), die die Ereignisschleife beendet und die Antwort nicht behandelt.

Um Ihre Antwort zu handhaben, wollen Sie den Anruf this.emit() halten:

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

Lesen Sie hier über das Programmiermodell für die Lambda-Funktionen unter NodeJS: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Verwandte Themen