javascript 猫鼬 - 为什么我们在设置猫鼬模块时制作“mongoose.Promise = global.Promise”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51862570/
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
Mongoose - Why we make "mongoose.Promise = global.Promise" when setting a mongoose module?
提问by Webwoman
I'm working with Mongoose. I have seen a lot of developers make the following command:
我正在与猫鼬一起工作。我看到很多开发人员发出以下命令:
mongoose.Promise = global.Promise;
Then I was curious to see what is the original value of mongoose.Promise. I have entered in my editor the following command:
然后我很好奇,看看mongoose.Promise. 我在编辑器中输入了以下命令:
const mongoose = require("mongoose");
console.log("promise: ", mongoose.Promise);
My console returned me :
我的控制台返回给我:
promise: function Promise() { [native code] }
承诺:函数 Promise() { [本机代码] }
Okay, so why make the command mongoose.Promise = global.Promisesince the Mongoose's promise already returns a native code ? I don't understand the point, if someone can help us to understand, would be great,
好的,mongoose.Promise = global.Promise既然 Mongoose 的承诺已经返回了本机代码,那么为什么要发出命令呢?我不明白这一点,如果有人能帮助我们理解,那就太好了,
Thanks
谢谢
回答by Estus Flask
This is legacy code from older examples that isn't needed with Mongoose 5.
这是来自旧示例的遗留代码,Mongoose 5 不需要。
Mongoose 4 relied on its own promise implementation, mpromise. mongoose.Promisewasn't necessarily Promiseglobal.
Mongoose 4 依赖于它自己的 promise 实现,mpromise. mongoose.Promise不一定是Promise全球性的。
As Mongoose 4 documentationstates:
正如猫鼬 4 文档所述:
Mongoose 5.0 will use native promises by default (or bluebird, if native promises are not present) but still support plugging in your own ES6-compatible promises library. Mongoose 5.0 will not support mpromise.
Mongoose 5.0 将默认使用本机承诺(或 bluebird,如果本机承诺不存在),但仍支持插入您自己的 ES6 兼容承诺库。Mongoose 5.0 将不支持 mpromise。
Though the statement about Bluebird is no longer true; Mongoose 5 dropped the support of Node versions that don't have native promises.
虽然关于 Bluebird 的说法不再正确;Mongoose 5 放弃了对没有原生 promise 的 Node 版本的支持。
mongoose.Promise = global.Promise;
may still be needed if global.Promisewas assigned with another implementation (e.g. Bluebird) after Mongoose was imported, though a better thing would be to assign global.Promise = Bluebirdearlier instead.
如果global.Promise在导入 Mongoose 后分配给另一个实现(例如 Bluebird),则可能仍然需要,尽管最好global.Promise = Bluebird提前分配。
回答by vkarpov15
Mongoose maintainer here. If you're using Mongoose 5, please remove mongoose.Promise = global.Promise;. That line was used to address the below deprecation warning with promises in Mongoose 4:
猫鼬维护者在这里。如果您使用的是 Mongoose 5,请删除mongoose.Promise = global.Promise;. 该行用于在 Mongoose 4 中通过承诺解决以下弃用警告:
WARNING: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead
It does nothing in Mongoose 5. You should only use mongoose.Promisein Mongoose 5 if you want to use your own promise librarywith Mongoose, like Bluebird or Q.
它在 Mongoose 5 中什么都不做。mongoose.Promise如果你想在 Mongoose 中使用你自己的承诺库,比如 Bluebird 或 Q,你应该只在 Mongoose 5 中使用。
There's also a little more about mongoose.Promisehere: https://masteringjs.io/tutorials/mongoose/promise#the-mongoosepromise-property
这里还有一点mongoose.Promise:https: //masteringjs.io/tutorials/mongoose/promise#the-mongoosepromise-property
回答by Azkri Said
If we want to use mongoose in different position inside the codes it must be viewed as global mode, that's why we need to set mongoose as :
如果我们想在代码中的不同位置使用 mongoose,它必须被视为全局模式,这就是为什么我们需要将 mongoose 设置为:
mongoose.Promise = global.Promise;
回答by Azkri Said
we used just in the point when we want to get connected in to MongoDB database :
我们只是在我们想要连接到 MongoDB 数据库时使用:
var mongoose = require('mongoose');
var mongoDB = ' database url ';
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
mongoose.connection.on('error',console.error.bind(console, 'MongoDB connection error:'));
you need to create schema and do your own model after
您需要创建架构并在之后做自己的模型
回答by mohammed alreai
First, mongoose uses Promise for Async applications. In new versions it can be:
首先,mongoose 将 Promise 用于异步应用程序。在新版本中,它可以是:
mongoose.connect("URL").then("what you need to happen after the connection is done").catch("when the error happened")
Therefore, I used promise and global to use mongoose anywhere as Async when writing mongoose.
因此,在编写 mongoose 时,我使用 promise 和 global 在任何地方使用 mongoose 作为 Async。

