user600314
user600314

Reputation: 662

How to use mongoose Promise - mongo

Can someone give me an example on how to use a Promise with mongoose. Here is what I have, but its not working as expected:

app.use(function (req, res, next) {
  res.local('myStuff', myLib.process(req.path, something));
  console.log(res.local('myStuff'));
  next();
});

and then in myLib, I would have something like this:

exports.process = function ( r, callback ) {
  var promise = new mongoose.Promise;
  if(callback) promise.addBack(callback);

  Content.find( {route : r }, function (err, docs) {
     promise.resolve.bind(promise)(err, docs);

  });

  return promise;

};

At some point I am expecting my data to be present, but how can I access it, or get at it?

Upvotes: 48

Views: 82429

Answers (6)

Mr.Thanks
Mr.Thanks

Reputation: 225

On this page:http://mongoosejs.com/docs/promises.html

The title is Plugging in your own Promises Library

Upvotes: 0

Alexey B.
Alexey B.

Reputation: 12033

Mongoose 4.0 Release Notes

Mongoose 4.0 brings some exciting new functionality: schema validation in the browser, query middleware, validation on update, and promises for async operations.

With [email protected] you can use any promises that you want

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

Another example with polyfilling global.Promise

require('es6-promise').polyfill();
var mongoose = require('mongoose');

So, you can just do later

Content
  .find({route : r})
  .then(function(docs) {}, function(err) {});

Or

Content
  .find({route : r})
  .then(function(docs) {})
  .catch(function(err) {});

P.S. Mongoose 5.0

Mongoose 5.0 will use native promises by default if available, otherwise no promises. You will still be able to set a custom promises library using mongoose.Promise = require('bluebird');, however, mpromise will not be supported.

Upvotes: 23

Anthony
Anthony

Reputation: 14269

Use the bluebird Promise library like this:

var Promise = require('bluebird');
var mongoose = require('mongoose');
var mongoose = Promise.promisifyAll(mongoose);

User.findAsync({}).then(function(users){
  console.log(users)
})

This is thenable, such as:

User.findAsync({}).then(function(users){
      console.log(users)
    }).then(function(){
      // more async stuff
    })

Upvotes: -1

Alexander Shtuchkin
Alexander Shtuchkin

Reputation: 1827

In the current version of Mongoose, the exec() method returns a Promise, so you can do the following:

exports.process = function(r) {
    return Content.find({route: r}).exec();
}

Then, when you would like to get the data, you should make it async:

app.use(function(req, res, next) {
     res.local('myStuff', myLib.process(req.path));
     res.local('myStuff')
         .then(function(doc) {  // <- this is the Promise interface.
             console.log(doc);
             next();
         }, function(err) {
             // handle error here.
         });
});

For more information about promises, there's a wonderful article that I recently read: http://spion.github.io/posts/why-i-am-switching-to-promises.html

Upvotes: 48

deedubs
deedubs

Reputation: 292

I believe you're looking for

exports.process = function ( r, callback ) {
  var promise = new mongoose.Promise;
  if(callback) promise.addBack(callback);

  Content.find( {route : r }, function (err, docs) {
     if(err) {
       promise.error(err);
       return;
     }
     promise.complete(docs);

  });

  return promise;

};

Upvotes: 5

konsumer
konsumer

Reputation: 3493

Mongoose already uses promises, when you call exec() on a query.

var promise = Content.find( {route : r }).exec();

Upvotes: 30

Related Questions