sql >> Base de Datos >  >> NoSQL >> MongoDB

Cómo usar la promesa de mangosta - mongo

En la versión actual de Mongoose, exec() El método devuelve una Promesa, por lo que puede hacer lo siguiente:

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

Luego, cuando desee obtener los datos, debe sincronizarlos:

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.
         });
});

Para obtener más información sobre las promesas, hay un artículo maravilloso que leí recientemente:http://spion.github.io/posts/why-i-am-switching-to-promises.html