我遇到了一个问题,我不确定采取什么措施来修复它。现在所有的数据都被正确检索,但是responseCallback永远不会被触发,因此我没有使用响应数组到达res.json调用。这里的任何指导将不胜感激!感谢。
为了澄清问题,问题在于aysnc.each回调。
$myFile = fopen('path/to/file', 'w') or die('Problems');
$client = new \GuzzleHttp\Client();
$request = $client->get('https://www.yourdocumentpage.com', ['save_to' => $myFile]);
这是我希望将更新的商业信息返回给客户的地方,但这永远不会触发。
var updatedBusinesses = [];
googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){
Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){return next(err);}
if(business !== null){
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){return next(err);}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback();
});
})
}
})
},function(err){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
答案 0 :(得分:1)
async.each()期望每次迭代都会调用一个回调(responseCallback)。如果没有打电话,它会坐在那里等待它。这就是为什么您的更新业务部分永远不会被调用的原因。
在async.each()中,有许多地方调用next()而不是async.each()迭代的回调(responseCallback)。以下是正确调用回调的修订代码:
var updatedBusinesses = [];
googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){
Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){
return responseCallback(err);// <== calling responseCallback instead of next()
}
// in case of business === null/undefined, I'm not seeing any
// callback getting called, it needs to be called inside
// async.each() no matter which condition it is
if (!business) {
// call responseCallback to continue on with async.each()
return responseCallback();
}
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){
return responseCallback(err);// <== calling responseCallback instead of next()
}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback();
});
})
})
},function(err){
if(err){return next(err);}
console.log("called");
res.json(updatedBusinesses);
});
});
所以现在为async.each()中的每个条件调用responseCallback()。它应该归结为更新的商业信息&#34;现在代码的一部分。
答案 1 :(得分:0)
使用responseCallback();
更改代码responseCallback(null, [your-result]);
我认为应该完成这项工作
var updatedBusinesses = []; googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){ Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){return next(err);}
if(business !== null){
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){return next(err);}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback(null, updatedBusinesses);
});
})
} }) },function(err, updatedBusinesses){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})