Mongoose and Q

There are some gotchas to working with Mongoose and Q. Here are a few things I've found that you need to be aware of...

Binding

Attempting to directly invoke a mongoose function with ncall or napply won't work because it looses its function binding. Instead, you will need to use the ninvoke method to call functions.

Q.invoke.find(Room, 'findById', id)  
.then(function(room) {
  // handle success
})
.fail(function(err) {
  // handle error
});

Multi-Result-Value Callbacks

When invoking the save operations... or any operation for that matter, you need to be aware that callbacks with multiple arguments will be converted to an Array.

Q.ninvoke.save(room, 'save')  
.then(function(saveResults) {
  var room = saveResults[0]
    , rowsAffected = saveResults[1];
});

So working with save will mean your result set will return 2 results... the object and the rows affected as per the documentation.

I'll be adding to this list as I encounter more...

comments powered by Disqus