JavaScript Build a REST API With Express Modeling Data for the API Connecting Mongoose to MongoDB

Edvards Valbahs
Edvards Valbahs
22,971 Points

WARNING: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead

the warning appears after successful connection:

db connection successful Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html Saved! db connection closed

POSSIBLE SOLUTION (in your js file add mongoose.Promise = global.Promise; line before mongoose.connect ...):

mongoose.Promise = global.Promise; mongoose.connect("mongodb://localhost:27017/sandbox");

2 Answers

Edvards Valbahs
Edvards Valbahs
22,971 Points

POSSIBLE SOLUTION (in your js file add mongoose.Promise = global.Promise; line before mongoose.connect ...):

mongoose.Promise = global.Promise; 
mongoose.connect("mongodb://localhost:27017/sandbox");

This is not really an answer, just more info about what the error itself for any students watching going forward. The version used in the video is 4.4, as of today (July 14, 2017) current version is 4.11.2, depreciation happened with version 4.11.

@Andrew - fyi for the next round of video updates - thank you.

Full Error

(node:9044) DeprecationWarning: `open()` is deprecated in mon
goose >= 4.11.0, use `openUri()` instead, or set the `useMong
oClient` option if using `connect()` or `createConnection()`.
 See http://mongoosejs.com/docs/connections.html#use-mongo-cl
ient
db connection was successful, connected to : [object Object]
(node:9044) DeprecationWarning: Mongoose: mpromise (mongoose'
s default promise library) is deprecated, plug in your own pr
omise library instead: http://mongoosejs.com/docs/promises.ht
m

From Mongoose site:

The useMongoClient Option

Mongoose's default connection logic is deprecated as of 4.11.0. Please opt in to the new connection logic using the useMongoClient option, but make sure you test your connections first if you're upgrading an existing codebase!

// Using `mongoose.connect`...
var promise = mongoose.connect('mongodb://localhost/myapp', {
  useMongoClient: true,
  /* other options */
});

// Or `createConnection`
var promise = mongoose.createConnection('mongodb://localhost/myapp', {
  useMongoClient: true,
  /* other options */
});
promise.then(function(db) {
  /* Use `db`, for instance `db.model()`
});

// Or, if you already have a connection
connection.openUri('mongodb://localhost/myapp', { /* options */ });

The parameters to openUri() are passed transparently to the underlying MongoDB driver's MongoClient.connect() function. Please see the driver documentation for this function for options. The same is true for connect() and createConnection() if useMongoClient is true.

This deprecation is because the MongoDB driver has deprecated an API that is critical to mongoose's connection logic to support MongoDB 3.6, see this github issue for more details.