Search code examples
node.jsmongodbmongoosenode-async

Mongoose remove in async.parallel


I don't quite understand, why this doesn't work:

async.parallel([
  SomeStuff.find({}).remove,
  SomeStuff2.find({}).remove,
  SomeStuff3.find({}).remove
], done);

while this works ok

async.parallel([
  function(callback) {
    SomeStuff.find({}).remove(callback)
  },
  function(callback) {
    SomeStuff2.find({}).remove(callback);
  },
  function(callback) {
    SomeStuff3.find({}).remove(callback);
  }
], done);

Isn't it practically the same? The first one throws an error:

TypeError: Object #<Object> has no method 'cast'
at Query.remove (/path/to/project/node_modules/mongoose/lib/query.js:1366:10)

Thank you for your help :)


Solution

  • The problem is that you're losing the function context when you pass the function itself like that. remove() is a prototype method, so it relies on the value of this being correct. When you pass the method by itself, the this value is now lost so it can no longer do things like this.cast().