Node.js RESTAPI在执行后续步骤之前响应等待

时间:2017-09-01 09:54:00

标签: node.js aws-lambda

我正在使用Amazon Lex。在Amazon Lambda中,我编写了一个Node.js函数,该函数调用Openweather API调用。这是功能。

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 ....如何阻止它执行。如果您查看下面的日志“回拨请求之前”在“回复请求之前”执行之前请帮我解决此问题?????

  

09:32:13 START RequestId:6fafb856-8ef8-11e7-8a17-afa62db3dcdc   版本:$LATEST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   9时32分13秒   2017-09-01T09:32:13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 里面   执行请求09:32:13   2017-09-01T09:32:13.735Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前   http执行请求 09:32:13   2017-09-01T09:32:13.975Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前   回叫请求09:32:14   2017-09-01T09:32:14.190Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc 之前   回应请求09:32:14   2017-09-01T09:32:14.193Z 6fafb856-8ef8-11e7-8a17-afa62db3dcdc {“coord”:{“lon”: - 71.32,“lat”:44.63},“weather”:[{“id”:804 ,“主”:“云”,“说明”:“阴   云”, “图标”: “04n”}], “基”: “站”, “主”:{ “温度”:280.34, “压力”:1015, “湿度”:70, “temp_min”:279.15, “temp_max”:281.15}, “可见性”:16093, “风”:{ “速度”:2.6, “DEG”:250}, “云”:{ “所有”:90}, “DT”:1504257840” SYS “:{” 类型 “: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

1 个答案:

答案 0 :(得分: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` }));
});

在这种情况下,只有在完成从API接收响应后才会调用lambda回调。

希望这有帮助!