75

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

Webwoman
  • 10,196
  • 12
  • 43
  • 87

5 Answers5

74

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 necessarily Promise global.

As Mongoose 4 documentation states:

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.

Though the statement about Bluebird is no longer true; Mongoose 5 dropped the support of Node versions that don't have native promises.

mongoose.Promise = global.Promise;

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 assign global.Promise = Bluebird earlier instead.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • 6
    Just to confirm, we don't need to use `mongoose.promise = global.promise`? We can use native promise? Also, By native promise you mean `.then`, `promise`, `promise.all `? – Alwaysblue Oct 18 '18 at 06:36
  • 6
    Yes, we don't need it. Other promise implementations may have compatible methods too. By native promise I mean that `model.save(...)` returns an instance of `Promise`. – Estus Flask Oct 18 '18 at 06:48
30

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:

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.Promise in Mongoose 5 if you want to use your own promise library with Mongoose, like Bluebird or Q.

There's also a little more about mongoose.Promise here: https://masteringjs.io/tutorials/mongoose/promise#the-mongoosepromise-property

vkarpov15
  • 3,614
  • 24
  • 21
3

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.Promise = global.Promise;
Til
  • 5,150
  • 13
  • 26
  • 34
Azkri Said
  • 31
  • 1
0

we used just in the point when we want to get connected in to MongoDB database :

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

kyun
  • 9,710
  • 9
  • 31
  • 66
Azkri Said
  • 31
  • 1
0

First, mongoose uses Promise for Async applications. In new versions it can be:

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.

Community
  • 1
  • 1
Mohammed Al-Reai
  • 2,344
  • 14
  • 18