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:
const mongoose = require("mongoose");
console.log("promise: ", mongoose.Promise);
My console returned me :
promise: function Promise() { [native code] }
Okay, so why make the command mongoose.Promise = global.Promise
since 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,
Thanks
we used just in the point when we want to get connected in to MongoDB database :
you need to create schema and do your own model after
This is legacy code from older examples that isn't needed with Mongoose 5.
Mongoose 4 relied on its own promise implementation,
mpromise
.mongoose.Promise
wasn't necessarilyPromise
global.As Mongoose 4 documentation states:
Though the statement about Bluebird is no longer true; Mongoose 5 dropped the support of Node versions that don't have native promises.
may still be needed if
global.Promise
was assigned with another implementation (e.g. Bluebird) after Mongoose was imported, though a better thing would be to assignglobal.Promise = Bluebird
earlier instead.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 :
First, mongoose uses Promise for Async applications. In new versions it can be:
Therefore, I used promise and global to use mongoose anywhere as Async when writing mongoose.