2017-09-01 4 views
0

Ich arbeite an Amazon Lex. In Amazon Lambda habe ich eine Node.js-Funktion geschrieben, die einen Openweather-API-Aufruf aufruft. Hier ist die Funktion.Node.js RESTAPI Antwort wartet auf die Ausführung der nächsten Schritte

function todaysweather(intentRequest, callback, data) { 
    const location = intentRequest.currentIntent.slots.location; 
    const source = intentRequest.invocationSource; 
    const outputSessionAttributes = intentRequest.sessionAttributes || {}; 
    const temp = JSON.parse(outputSessionAttributes.temp || '{}'); 
    ***console.log("Inside perform request");*** 
    var endpoint = '/data/2.5/weather?q=${location}&appid=234426bef0d81ef4474073344f'; 
    var method = 'POST'; 
    var dataString = JSON.stringify(data); 
    var headers = {}; 
    var responseObject; 

    if (method == 'GET') { 
    endpoint += '?' + querystring.stringify(data); 
    } 
    else { 
    headers = { 
     'Content-Type': 'application/json', 
     'Content-Length': dataString.length 
    }; 
    } 
    var options = { 
    host: host, 
    path: endpoint, 
    method: method, 
    headers: headers 
    }; 
    ***console.log("Before http perform request");*** 
    var req = https.request(options, function(res) { 
    res.setEncoding('utf-8'); 
    var responseString = ''; 
    res.on('data', function(data) { 
     responseString += data; 
    }); 
    ***console.log("before perform response");*** 
    res.on('end', function() { 
     console.log(responseString); 
     responseObject = JSON.parse(responseString); 
     var tempVAR = responseObject.main.temp; 
     console.log("*************" + tempVAR); 
     //success(responseObject); 
    }); 
    }); 

    req.write(dataString); 
    req.end(); 

    ***console.log("before callback request");*** 
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', 
     content: `Okay, I have booked your appointment. We will see you` })); 
} 

Der API-Aufruf nimmt wenige Millisekunden zurück zu reagieren ... vor, dass mein nächster Code exectute bekommen .... Wie es von der Ausführung zu stoppen. Wenn Sie sich die Protokolle ansehen, wird "vor der Rückrufanfrage" vor "vor der Antwortanfrage" ausgeführt. Helfen Sie mir bei der Lösung dieses Problems ?????

 09.32.13 START RequestId: 6fafb856-8ef8-11e7-8a17-afa62db3dcdc Version: $ AKTUELLE  09.32.13 2017-09-01T09: 32: 13.734Z 6fafb856-8ef8- 11e7-8a17-afa62db3dcdc event.bot.name = TodaysWeather  09.32.13 2017-09-01T09: 32: 13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc Dispatch userid = yczl74p0he593v0kduouy5c5nhci50e5, intentName = Todaysweather  09.32.13 2017-09-01T09: 32: 13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc Innen Anfrage  09.32.13 2017-09-01T09 ausführen: 32: 13,735 Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc Bevor http Anfrage durchführen  09.32.13 2017-09-01T09: 32: 13.975Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc vor Rückrufanfrage  09.32.14 2017-09-01T09: 32: 14.190Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc vor Antwortanforderung  09.32.14 2017-09-01T09: 32: 14.193Z 6fafb856- 8ef8-11e7-8a17-afa62db3dcdc {"coord": {"lon": - 71.32, "lat": 44.63}, "Wetter": [{"id": 804, "main": "Clouds", "description" Wolken "," Symbol ":" 04n "}]," Basis ":" Stationen "," Haupt ": {" temp ": 280.34," Druck ": 1015," Feuchte ": 70," temp_min ": 279.15," temp_max ": 281.15}," Sichtbarkeit ": 16093," wind ": {"Geschwindigkeit": 2.6, "Grad": 250}, "Wolken": {"alle": 90}, "dt": 1504257840, "sys": {"Typ": 1, "id": 1956  09:32:14 2017-09-01T09: 32: 14.233Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc ************* 280.34  09:32:14 ENDE RequestId: 6fafb856 -8ef8-11e7-8a17-afa62db3dcdc

Antwort

0

Ja, dies ist wegen der Node.js asynchronen Natur. Sie senden die Anfrage und rufen danach den Rückruf an, ohne auf die Antwort zu warten.

Um auf Antwort zu warten, müssen Sie den Rückruf Callback im Rückruf von res.on('end',.. aufrufen. Etwas wie:

res.on('end', function() { 
    responseObject = JSON.parse(responseString); 
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', content: `Okay, I have booked your appointment. We will see you` })); 
}); 

In diesem Fall werden Sie nur den Lambda-Rückruf anrufen, wenn Sie die Antwort des API fertig receiing.

Hoffe, das hilft!

+0

Danke King .. Lösung ist hilfreich – bgara

+0

@ user3083804 Akzeptieren Sie die Antwort, wenn es Ihre Abfrage löst. – tbking

Verwandte Themen