0

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};
4
  • How about acutally showing you "Callback hell" code! Then rather that just asking "The way I tried did not work :(" then someone could take a look and possibly suggest even another better alternative. Imagine that! Also please describe the actual problem your code is meant to solve, since more often than not another set of eyes will likely see an even better approach yet again. "Fix my syntax" questions never get the most optimal answers. Commented Mar 10, 2016 at 0:21
  • After capCampaign there are some 6 functions waterfalling.So, instead of 6-fold indentation I want named functions on the same paragraph. This is exactly async.waterfall, no? Commented Mar 10, 2016 at 0:40
  • Was kind of where I was going with the latter part of the comment. If you are really reading a result and then have 6 following async calls, that screams out "rethink" to me. The only places I have anything like 6 calls running in any code I have ever written are where the calls are in fact in parallel, and never in series such as waterfall implies. Commented Mar 10, 2016 at 0:50
  • Well, first I aggregate campaigns (mainly filters through $match), out of them I find marked campaigns for which I should do a call to remote server and update them for prices. then I filter the campaigns I've got based on cookie values - this is just cycling over array of campaigns I have. This filtering function does not require IO but I yet put it in waterfall for unification. Then, if after that filter no campaigns remained I call again to DB to get default campaign. Actually, it's less than 6 funcs in series. Does it seem to you incorrect to use waterfall ? Commented Mar 10, 2016 at 1:14

1 Answer 1

1

Your code here will immediately apply the pipeline to the aggregate method, also, there is no way for it to callback in your code. Try this instead:

async.waterfall(
  [
    function(cb) { return db.collection('campaigns').aggregate(pipeline, cb) },
    capCampaigns,
  ], 
  function(err, result) {
  }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.