node.js 如何使用猫鼬 Promise - mongo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9022099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:04:40  来源:igfitidea点击:

How to use mongoose Promise - mongo

node.jsmongodbexpressmongoosepromise

提问by user600314

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:

有人能给我一个关于如何使用 Promise 和 mongoose 的例子吗?这是我所拥有的,但它没有按预期工作:

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:

然后在 myLib 中,我会有这样的事情:

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?

在某些时候,我希望我的数据存在,但我如何访问或获取它?

回答by Alexander Shtuchkin

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

在当前版本的 Mongoose 中,该exec()方法返回一个 Promise,因此您可以执行以下操作:

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

有关 promise 的更多信息,我最近阅读了一篇精彩的文章:http: //spion.github.io/posts/why-i-am-switching-to-promises.html

回答by konsumer

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

当您调用exec()查询时,Mongoose 已经使用了 Promise。

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

回答by Alexey B.

Mongoose 4.0 Release Notes

猫鼬 4.0 发行说明

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

Mongoose 4.0 带来了一些令人兴奋的新功能:浏览器中的模式验证、查询中间件、更新验证和异步操作承诺

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

使用[email protected]您可以使用任何您想要的承诺

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

Another example with polyfilling global.Promise

另一个 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

PS猫鼬5.0

Mongoose 5.0 will use native promises by defaultif 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.

如果可用,Mongoose 5.0 将默认使用本机承诺,否则不使用承诺。您仍然可以使用 设置自定义承诺库mongoose.Promise = require('bluebird');,但是,将不支持 mpromise。

回答by deedubs

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;

};

回答by Mr.Thanks

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

在此页面上:http: //mongoosejs.com/docs/promises.html

The title is Plugging in your own Promises Library

标题是插入你自己的 Promises 库

回答by Anthony

Use the bluebird Promise library like this:

像这样使用 bluebird Promise 库:

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