0
我想從For循環的最後一個值發送響應(res.send)。如果我給res.send totalCost函數我得到的錯誤響應不能發送。請幫我發送回覆。如何從節點js中的for循環獲取值?
viewCampaign.aggregate([
{$group:{_id:"$campaign",count:{$sum:1}}}
],function(err,arr){
if(err){
console.log(err)
} else {
console.log("arr",arr);
arr.forEach(function (val,index) {
var cid=arr[index]._id
var adCount=arr[index].count
totalCost(cid,adCount)
})
var totalAdCost=0
var totalEarn={}
function totalCost(cid,adCount){
console.log("adCount",adCount)
console.log(typeof(cid), "cid",cid)
campaignList.findOne({_id:cid},function(err,campaignDetails){
if(err){
console.log(err)
} else {
console.log("campaignDetails",campaignDetails.cost)
var costVal=totalAdCost+campaignDetails.cost*adCount
totalAdCost=costVal
}
})
}
}
})
http://stackoverflow.com/questions/29738535/catch-foreach-last-iteration – dmoo