2016-12-05 83 views
1

我使用Express.js作爲我的應用程序的中間件。我的前端應用程序每隔幾秒用不同的參數調用一條快速路由。然後路由將每個參數調用到第三方api。在每個調用內部,數據都被添加到數組中。在for循環之後,我將數組發送回前端。Express JS發送數組始終爲空

我在路由中聲明數組,但返回的結果是空的。當我在路由之外聲明數組時,它正在工作,但當多個請求同時發生時,這不是最佳的。

server.js

app.get('/prices', function(req, res){ 

//array for holding all feeds 
var feedData = []; 

//Url params 
var target = req.param('target'); 

//Convert the params to an array 
var targetArray = target.split(','); 

//Loop through targets and do request 
for (var i = 0; i < targetArray.length; i++) { 

    //API endpoint 
    var url = xxx; 
    request(url, function (error, response, body) { 

    //Parse the XML body to javascript objects or json 
    parseString(body, function (err, result) { 


    //Store result in js object 
    var resultObject = result; 
    var arrayObject = resultObject.quotes.quote; 

    feedData.push(arrayObject[0]) 

    }); 

}); 
} 
console.log(feedData); 
res.send(feedData); 

//Clear feedData 
feedData = []; 


}); 

所以底線是,數組爲空。有什麼建議麼?

+1

'異步'請求 – Weedoze

回答

3

你被JS節點(當然,JavaScript)的

request的調用是異步的,並接受回調,但你踢腳過它,所以你最終console.log(feedData);等的異步性質咬傷呼叫在調用request的回調之前被調用。

看看在async模塊,特別是eachSeries

你的代碼的非測試例如,使用異步庫:

app.get('/prices', function(req, res) { 

    //Url params 
    var target = req.param('target'); 

    //Convert the params to an array 
    var targetArray = target.split(','); 

    //array for holding all feeds 
    var feedData = []; 

    async.eachSeries(targetArray, function(targetArrayItem, cb) { 

     request(url, function(error, response, body) { 
      //Parse the XML body to javascript objects or json 
      parseString(body, function(err, result) { 


       //Store result in js object 
       var resultObject = result; 
       var arrayObject = resultObject.quotes.quote; 

       feedData.push(arrayObject[0]) 

       //call the callback to iterate next item in targetArray 
       cb(); 
      }); 
     }); 

    }, function(err) { 
     //all done 

     console.log(feedData); 
     res.send(feedData); 

     //Clear feedData 
     feedData = []; 
    }) 
}); 
+1

謝謝。我感覺我被異步列車撞到了,只是無法弄清楚。現在有道理。 – TietjeDK

+0

沒有問題,很高興它有幫助 – Alex

0

這是因爲JavaScript是異步的,你發送的數據在準備就緒之前,您需要檢查上次請求響應並返回收集的數據。

app.get('/prices', function(req, res){ 

//array for holding all feeds 
var feedData = []; 

//Url params 
var target = req.param('target'); 

//Convert the params to an array 
var targetArray = target.split(','); 

//Loop through targets and do request 
for (var i = 0; i < targetArray.length; i++) { 

    //API endpoint 
    var url = xxx; 
    request(url, function (error, response, body) { 

    //Parse the XML body to javascript objects or json 
    parseString(body, function (err, result) { 


    //Store result in js object 
    var resultObject = result; 
    var arrayObject = resultObject.quotes.quote; 

    feedData.push(arrayObject[0]); 

    //If is last request return result 
    if(i==targetArray.length-1){ 
      console.log(feedData); 
      return res.send(feedData); 
    } 
    }); 

}); 
} 
}); 
+0

這不會工作,你'targetArray'超出'request'的回調 - 沒有什麼迭代for循環 – Alex