Mongoose: Sometimes I still have a DeprecationWarning: Mongoose: mpromise.

Created on 25 Feb 2017  路  10Comments  路  Source: Automattic/mongoose

I have a such warning in my code:

(node:78516) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

But in the very beginning I set right Promise like this: mongoose.Promise = global.Promise;

Sorry, I can't give you a small completed example.js to reproduce this problem. I have a mono repository for microservices, and only one of them has a such warning.

But I hope the next code will help you to understand the problem:

In the very beginning of my app I have mongoose.Promise = global.Promise;

then, the code:

  console.log('---BEGIN---');
  const event = await Event.findById(id);
  console.log('find is ok, without warning');
  console.dir(mongoose.Promise);
  await event.save();
  console.dir(mongoose.Promise);
  console.log('---END---');

This code logs this:

find is ok, without warning
{ [Function: ES6Promise] use: [Function], ES6: [Function: Promise] }
(node:79712) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
{ [Function: ES6Promise] use: [Function], ES6: [Function: Promise] }
---END---

So, logs say this:
1) when we use Mongoose to find, there are no any warnings
2) we are sure that we use ES6Promises
3) but when we use .save() method for model, we have the warning.

Everything works fine. But there is only a such warning for only one microservice, other microservices don't have the warning.

What is going on with it?

P.S. [email protected], [email protected], [email protected]

All 10 comments

@serge-nikitin your Event model was created before you set mongoose.Promise = global.Promise. You need to set mongoose.Promise before you create any schemas/models.

@varunjayaraman No, I set mongoose.Promise = global.Promise in the very beginning in my applications. In that code it was a second settings of mongoose.Promise. Sorry, it was confusing, I will update my code in the post now.

I'm having same issue. Getting the warning even though I set mongoose.Promise = global.Promise in the very beginning of my app.

I'm gonna need to see a better code sample then, how the schema that is calling findById is created, etc.

@varunjayaraman Here is a code of Event model, it looks like a normal model:

const mongoose = require('mongoose');
const c = require('../c');

const schema = new mongoose.Schema({
  _id: { type: Number },
  date: { type: Date, required: true, index: true },
  name: { type: String, required: true },
  league: { type: String, required: true },
  html: { type: String, default: '' },
  htmlStatus: { type: String, required: true, index: true, default: c.HTML_STATUS.NEW, enum: Object.values(c.HTML_STATUS) },
  parseStatus: { type: String, required: true, index: true, default: c.PARSE_STATUS.NEW, enum: Object.values(c.PARSE_STATUS) },
  score: { type: String, default: '' },
  actions: { type: [String], default: [] }
}, { collection: 'events' });

schema.virtual('htmlStatusColor').get(function() {
  if (this.htmlStatus === c.HTML_STATUS.OK) return 'green';
  if ([c.HTML_STATUS.UNAVAILABLE].includes(this.htmlStatus)) return 'orange';
  return 'red';
});

schema.virtual('parseStatusColor').get(function() {
  if ([c.PARSE_STATUS.OK, c.PARSE_STATUS.NO_NEED].includes(this.parseStatus)) return 'green';
  if ([c.PARSE_STATUS.DANG_ATT, c.PARSE_STATUS.EMPTY].includes(this.parseStatus)) return 'orange';
  return 'red';
});

const Event = mongoose.model('Event', schema);

module.exports = Event;

Sorry, I can't give you a short example.js to reproduce this problem. I've tried to do it, but I've failed. I have a large monorepository for microservices, they have a lot of /commons code. And all of them are written in the same form. And only one microservice has a such problem with warning.

ok, I'd need to see the root of the app. Basically I want to make sure that the mongoose you are require()ing in has its Promise constructor set before this file executes

same. didnt occur when I was on 4.7.x, but now with 4.8.x

Seems it similar to this issue: https://github.com/Automattic/mongoose/issues/4951 :octocat:

Same here, even if

const mongoose = require('mongoose')
mongoose.Promise = global.Promise

are the first two lines in the entry of the application, warning is still thrown in the console.

Likely a dupe of #5030. Try upgrading to >= 4.9.1.

Was this page helpful?
0 / 5 - 0 ratings