我有一个带有 async.map 的函数,它一直工作到地图的回调然后我得到这个错误:
function findSolutionsOneWay(tw, urls, isOutbound, callback){
async.map(urls, function(url, next) {
getRailFeed(url, function(err, responseJson){
if(err)
{
console.log(TAG + '@findSolutionsOneWay, err: ' + err);
console.log(TAG + '@findSolutionsOneWay, url: ' + url);
return next(null, ''); // donT break
}
//success
return next(null, responseJson);
});
},
// async callback
function(err, listResponseJson){
if(err){
console.log(TAG + '@findSolutionsOneWay failed: ' + aReq.toString());
return callback(err);
}
console.log(TAG + '@findSolutionsOneWay, isOutbound ' + isOutbound);
console.log(TAG + '@findSolutionsOneWay, listResponseJson size ' + listResponseJson.length);
var oneWaySolutions = [];
for(var i=0; i<listResponseJson.length; i++){
console.log(TAG + '@findSolutionsOneWay, # # # # # # processing response # : '+ i);
evaluateOneWaySolutions(JSON.parse(listResponseJson[i]), oneWaySolutions, isOutbound, tw);
}
console.log(TAG + '@findSolutionsOneWay, total # good solutions after evaluation: ' + oneWaySolutions.length);
callback(null, oneWaySolutions);
});
}
我的日志控制台输出:
RailManager@findSolutionsOneWay, listResponseJson size 3
RailManager@findSolutionsOneWay, # # # # # # processing response # : 0
RailManager@evaluateOneWaySolutions, no results to evaluate!
RailManager@findSolutionsOneWay, # # # # # # processing response # : 1
RailManager@evaluateOneWaySolutions, isOutbound true
RailManager@evaluateOneWaySolutions, solutionList.length 6
RailManager@findSolutionsOneWay, # # # # # # processing response # : 2
RailManager@evaluateOneWaySolutions, no results to evaluate!
RailManager@findSolutionsOneWay, total # good solutions after evaluation: 6
SearchTravelSolution.js: Cannot read property 'map' of undefined
TypeError: Cannot read property 'map' of undefined
at _map (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\node_modules\async\lib\async.js:54:16)
at _asyncMap (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\node_modules\async\lib\async.js:237:15)
at Object.map (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\node_modules\async\lib\async.js:219:23)
at findSolutionsOneWay (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\backgroundProcesses\searchTravelSolutions\RailManager.js:240:11)
at D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\backgroundProcesses\searchTravelSolutions\RailManager.js:75:13
at fn (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\node_modules\async\lib\async.js:641:34)
at Object._onImmediate (D:\Userfiles\oozen\Workspace\sas_1.0_server\sas_1.0\node_modules\async\lib\async.js:557:34)
at processImmediate [as _immediateCallback] (timers.js:345:15)
我错过了什么?
编辑:调用的函数findSolutionsOneWay
var findSolutions = function(tw){
async.waterfall([
function(callback) {
// FIND ORIGIN STATIONS
retrieveOriginRailStationCodes(tw, callback);
}, function(vectorOriginStations, callback) {
// FIND DESTINATION STATIONS
retrieveDestinationRailStationCodes(tw, function(err, vectorDestinationStations){
callback(err, vectorOriginStations, vectorDestinationStations);
});
}, function(vectorOriginStations, vectorDestinationStations, callback){
buildRailURLs(tw, vectorOriginStations, vectorDestinationStations, callback);
}, function(urls, callback){
// FIND OUTBOUND SOLUTIONS
findSolutionsOneWay(tw, urls.OutboundURLs, true, function(err, outboundSolutions){
callback(err, urls, outboundSolutions);
});
}, function( urls, outboundSolutions, callback){
if(outboundSolutions.length == 0) {return next('no solutions found');}
// FIND INBOUND SOLUTIONS
findSolutionsOneWay(tw, urls.vectorInboundURLs, false, function(err, inboundSolutions){
callback(err, outboundSolutions, inboundSolutions);
});
}
], function(err, outboundSolutions, inboundSolutions, tw) {
console.log(TAG + '@findSolutions, # of outboundSolutions: ' + outboundSolutions.length);
console.log(TAG + '@findSolutions, # of inboundSolutions : ' + inboundSolutions.length);
var hasHotel = JsonInfo.getHasHotel(tw);
if(hasHotel){
retrieveHotelSolutions(outboundSolutions, inboundSolutions, tw); //TODO: the rest is without async.
}
else{
console.log(' Error occurred! canT search for trains without hotel!')
}
});
}