更新时间:2023-02-17 12:20:23
在当前版本的 Mongoose 中,exec()
方法返回一个 Promise,因此您可以执行以下操作:
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.
});
});
关于 Promise 的更多信息,我最近阅读了一篇精彩的文章:http://spion.github.io/posts/why-i-am-切换到promises.html
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