How to use mongoose Promise – mongo

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)