I try to convert my callback hell code to more structured with the aid of async lib. Now, I have the following code:
db.collection('campaigns').aggregate(pipeline, function(err, campaigns) {
capCampaigns(campaigns, function(err, cappedCampaigns) {
// etc
}
});
I rewrite it as
async.waterfall([
async.apply(db.collection('campaigns').aggregate, pipeline),
capCampaigns,
// etc
], function(err, result) {
}
Sadly, async.apply fails:
TypeError: Cannot read property 's' of null
at Collection.aggregate (/home/beryllium/sitesrv/node_modules/mongodb/lib/collection.js:2458:35)
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:760:23
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:166:37
at fn (/home/beryllium/sitesrv/node_modules/async/lib/async.js:746:34)
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:1213:16
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:166:37
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:706:43
at /home/beryllium/sitesrv/node_modules/async/lib/async.js:167:37
at Object.async.waterfall (/home/beryllium/sitesrv/node_modules/async/lib/async.js:710:44)
at chooseCampaignAndBanner (/home/beryllium/sitesrv/routes/banner.js:207:9)
What do I do incorrectly?
Additional info: Node version: 5.7.1 MonogDB driver for NOdeJS: 2.1.2
The line at which mongo fails:
// Build the command
var command = { aggregate : this.s.name, pipeline : pipeline};