2016-07-08 44 views
0

我正在開發一個項目,我需要向第三方API發出多個請求,然後將收到的數據發回給客戶。顯然,對第三方API的請求是異步的,如果我在請求循環之後立即放置res.json,數據將是空的。我是否需要在承諾中包裝請求?這裏是我的代碼:如何在向第三方API發出多個請求後發送響應Node.js

const historicalForecast = (req, res, next) => { 
    console.log(req.body); 
    // GET COORDS FROM GOOGLE API BY LOCATION INPUT BY USER 
    let googleUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${req.body.input}&key=${googleKey}`; 
    request(googleUrl, function(error, response, body){ 
    if(error){ 
     console.log(error); 
     next(); 
    } 
    let data = JSON.parse(response.body); 
    //IF MORE THAN ONE RESULT FROM GEOLOCATION QUERY 
    //ADD DATES REQUESTED INTO RESPONSE AND 
    //SEND LIST OF LOCATIONS BACK SO USER CAN CHOOSE 
    if(data.results.length > 1){ 
     response.body.startDate = req.body.startDate; 
     response.body.endDate = req.body.endDate; 
     res.json(JSON.parse(response.body)); 

    //IF ONE RESULT, GET DATA IN BATCHES 
    }else if(data.results.length === 1) { 
     let coords = data.results[0].geometry.location; 
     const OneDay = 86400; 
     let timeFrame = Math.abs(req.body.startDate - req.body.endDate); 
     let numberOfDays = timeFrame/OneDay; 
     console.log(numberOfDays); 
     let results = []; 

     for(let i = 0; i < numberOfDays; i++){ 
     let currentDay = Number(req.body.startDate) + (i*OneDay); 
     let urlWeather = `https://api.forecast.io/forecast/${weatherKey}/${coords.lat},${coords.lng},${currentDay}`; 
     request(urlWeather, function(error, response, body){ 
      if(error){ 
      console.log(error); 
      next(); 
      } 
      results.push(JSON.parse(response.body)); 
      res.send(results); 
     }); 
     } 
    } 
    }); 
}; 
+2

你將要使用request'的'一個promisified版本,然後使用'Promise.all()'當所有請求知道完成。這個問題之前可能已經被提過很多次了,所以這裏可能有很多其他的解釋。 – jfriend00

+1

下面是使用承諾知道何時完成多個異步操作的一般結構:http://stackoverflow.com/questions/32799672/node-js-how-to-set-a-variable-outside-the-current-scope/32799727#32799727 – jfriend00

+0

謝謝你的建議。 @ jfriend00 – Atache

回答

0

按@ jfriend00的建議,我看了看:

Node.JS How to set a variable outside the current scope

,並用於提供有衆多選項之一。我的解決方案在上面的文章中。將書籤該帖子以備將來參考。

我更換了所有的代碼在else if語句:

let coords = data.results[0].geometry.location; 
    const OneDay = 86400; 
    let timeFrame = Math.abs(req.body.startDate - req.body.endDate); 
    let numberOfDays = timeFrame/OneDay; 


    const makeMultipleQueries = (url) => { 
    return new Promise(function(resolve, reject) { 
     request(url, function(error, response, body){ 
     if(error){ 
      reject(error); 
     } 
     resolve(response.body); 
     }); 
    }); 
}; 

let promises = []; 
for (let i = 0; i < numberOfDays; i++) { 
    let currentDay = Number(req.body.startDate) + (i*OneDay); 
    let url = `https://api.forecast.io/forecast/${weatherKey}/${coords.lat},${coords.lng},${currentDay}`; 
    promises.push(makeMultipleQueries(url)); 
} 
Promise.all(promises).then(function(results) { 
    res.json(results); 
}, function(err) { 
    next(err); 
}); 
+1

使用堆棧溢出的正確方法是將您的實際編碼答案放入您的答案中,並將其從您的問題中刪除。與其他一些網站不同,問題應該只是問題,答案應該只是答案。因此,請將您的解決方案置於您的問題之外,並將其放在您的答案中。然後,您可以「接受」您自己對問題的回答(一旦經過了一段時間),向社區表明您的問題現在已經得到解答。你也可以上傳顯示你方式的鏈接答案。 – jfriend00

相關問題