2017-09-01 25 views
0

我正在使用Amazon Lex。在Amazon Lambda中,我编写了一个调用Openweather API调用的Node.js函数。这是功能。Node.js RESTAPI REsponse在执行下一步之前等待

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

API调用需要几毫秒的时间来回应...在我的下一个代码越来越exectute ....如何阻止它执行。如果你看下面的“回调请求之前”的日志在“响应请求之前”执行之前请帮助我解决这个问题?

9时32分13秒开始的requestId:6fafb856-8ef8-11e7-8a17-afa62db3dcdc 版本:$最新9时32分13秒 2017-09-01T09:32:13.734Z 6fafb856-8ef8- 11e7-8a17-afa62db3dcdc event.bot.name = TodaysWeather 九点32分13秒 2017-09-01T09:32:13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc调度 用户id = yczl74p0he593v0kduouy5c5nhci50e5,intentName = Todaysweather 09:32:13 2017-09-01T09:32:13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc Inside 执行请求09:32:13 2017-09-01T09:32:13.735 ž6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前 HTTP执行请求9时32分13秒 2017-09-01T09:32:13.975Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前 回叫请求 9时32分14秒 2017-09-01T09:32:14.190Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前 响应请求9时32分14秒 2017-09-01T09:32:14.193Z 6fafb856- 8ef8-11e7-8a17-afa62db3dcdc {“coord”:{“lon”: - 71.32,“lat”:44.63},“weather”:[{“id”:804,“main”:“Clouds” :“阴云 云”,“icon”:“04n”}],“base”:“站”,“main”:{“temp”:280.34,“pressure”:1015,“humidity”:70,“temp_min “:279.15,” temp_max “:281.15},” 可见性 “:16093,” 风声“: “speed”:2.6,“deg”:250},“clouds”:{“all”:90},“dt”:1504257840,“sys”:{“type”:1,“id”:1956 09:32:14 2017-09-01T09:32:14.233Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc ************* 280.34 09:32:14 END RequestId:6fafb856 -8ef8-11e7-8a17-afa62db3dcdc

回答

0

是的,这是因为Node.js异步性质。您正在发送请求,并在此之后调用回调,而不等待响应。

要等待响应,您需要res.on('end',..的回调中调用lambda回调。喜欢的东西:

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

在这种情况下,你会打电话,只有当你完成receiing从API响应的拉姆达回调。

希望这会有所帮助!

+0

谢谢国王..解决方案很有帮助 – bgara

+0

@ user3083804如果解决了您的查询,请接受答案。 – tbking

相关问题