2017-09-01 89 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

相關問題