未达到异步回调,响应挂起
Not reaching aync callback, response hangs
我 运行 遇到了一个问题,我不确定要采取什么步骤来解决它。现在所有数据都被正确检索,但是 responseCallback 永远不会触发,因此我没有到达 res.json 调用响应数组。这里的任何指导将不胜感激!谢谢
澄清一下,问题出在 aysnc.each 回调上。
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);
})
})
这是我希望 return 向客户更新的业务信息的地方,但这永远不会触发。
},function(err){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
用 responseCallback(null, [your-result]);
更改您的代码 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 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);
})
})
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()。现在应该归结为 "updated business information" 部分代码。
我 运行 遇到了一个问题,我不确定要采取什么步骤来解决它。现在所有数据都被正确检索,但是 responseCallback 永远不会触发,因此我没有到达 res.json 调用响应数组。这里的任何指导将不胜感激!谢谢
澄清一下,问题出在 aysnc.each 回调上。
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);
})
})
这是我希望 return 向客户更新的业务信息的地方,但这永远不会触发。
},function(err){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
用 responseCallback(null, [your-result]);
更改您的代码 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 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);
})
})
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()。现在应该归结为 "updated business information" 部分代码。