61

I am using mongoose and mocha for MongoDB schema design and API development I am getting this warning... what does this mean, how it will affect me and what is the fix??

Below the actual warning text:

(node:9872) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.

Avani Khabiya
  • 1,207
  • 2
  • 15
  • 34
Kritagya Khandelwal
  • 775
  • 1
  • 6
  • 12
  • 1
    Does this answer your question? [How can I remove this deprecation warning in MongoDB and why is it happening?](https://stackoverflow.com/questions/66179884/how-can-i-remove-this-deprecation-warning-in-mongodb-and-why-is-it-happening) – Peter O. Feb 19 '21 at 14:26

6 Answers6

77

UPDATE

mongodb@3.6.5 is out.

Just update mongodb driver and mongoose:

npm i mongodb mongoose

This is caused by the mongodb@3.6.4 native driver which is used by mongoose.

#1 You can downgrade mongodb to version 3.6.3 (described here).

#2 Or downgrade mongoose from 5.11.16 back to 5.11.15:

npm uninstall mongoose
npm install mongoose@5.11.15

#3 Or just wait for the release of mongodb@3.6.5.

tom
  • 9,550
  • 6
  • 30
  • 49
  • I think you meant `3.6.4` and not `2.6.4` – Amrita Yadav Feb 15 '21 at 07:42
  • 2
    @kmgt I am getting this error with mongoose version `^5.10.12` and Mongodb version `4.4` What should be the take? Still need to go with mongoose version `5.11.15`? – Avani Khabiya Feb 17 '21 at 08:15
  • Okay. I considered it as Mongodb version, it is the mongodb driver version. Yes, the installed mongodb driver version is `3.6.3`. – Avani Khabiya Feb 17 '21 at 08:44
  • @kmgt is this warning safe to ignore or do I need to downgrade Mongoose? As downgrading Mongodb driver is not the option, I already have version `3.6.3` installed. – Avani Khabiya Feb 17 '21 at 08:48
  • I see. MongoDB 4.4! You can ignore it [link](https://developer.mongodb.com/community/forums/t/warning-accessing-non-existent-property-mongoerror-of-module-exports-inside-circular-dependency/15411/6). – tom Feb 17 '21 at 08:55
  • The link you shared is of some other warning `Warning: Accessing non-existent property ‘MongoError’ of module exports inside circular dependency`. The last comment which includes the above error isn't acknowledged by Mongodb concerned person yet. – Avani Khabiya Feb 17 '21 at 09:18
  • 2
    There is a dependency. [Study this](https://github.com/Automattic/mongoose/issues/9930). – tom Feb 17 '21 at 09:34
11

For all those looking for an answer for this, there is a question posted on Mongodb forum and the answer is acknowledged by Mongodb concerned person.

The answer given by @kmgt is correct. The error can be ignored safely and will be fixed in upcoming release of Mongodb Nodejs driver or downgrading Mongoose version to 5.11.15 will help.

(node:44612) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version

Avani Khabiya
  • 1,207
  • 2
  • 15
  • 34
8

Same Error enter image description here

But As far as I know its a new version compatibility bug, after searching about this current bug version, I found this comment session and according to one of them, it is safe to ignore this warning.

Marvin
  • 647
  • 7
  • 15
3

If you are using mongoose version 5.11.16 or a higher version, you will see this error. You can solve this issue by downgrading it to version 5.11.15.

npm uninstall mongoose

npm i mongoose@5.11.15

Although this is being discussed in the community and many comment sections and it is a warning because of a compatibility bug and it might not harm to ignore it. It is suggested that they will fix it in the next update.

hersheys17
  • 61
  • 3
2

You can use mongoose version 5.11.13

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 3
    Downgrade to mongoose 5.11.15 is sufficient. According to the [changelog](https://github.com/Automattic/mongoose/blob/master/History.md) "upgrade to mongodb driver 3.6.4" happened in 5.11.16. – Michael Geier Feb 16 '21 at 12:21
1

OK so I found a fix for this problem.

Task : Upload Image on mongoDB as binary data, using buckets, chunks etc.

Reference URL

Code:

const express = require("express");
const router = express.Router();
const User = require('../models/user');
const grid = require('gridfs-stream');
const GridFsStorage = require('multer-gridfs-storage');
const util = require("util");
const crypto = require('crypto');
const path = require('path');
const methodOverride = require('method-override');
const bodyParser = require('body-parser');
const dotenv = require("dotenv").config({path: "./config/config.env"});
const multer = require('multer');


const storage = new GridFsStorage({
    url: process.env.MONGO_URI,

    options: {
        useUnifiedTopology: true
    },
    file: (req, file) => {
        return new Promise((resolve, reject) => {
            crypto.randomBytes(16, (err, buf) => {
                if (err) {
                    return reject(err);
                }
                const filename = buf.toString('hex') + path.extname(file.originalname);
                const fileInfo = {
                    filename: filename,
                    bucketName: 'uploads'
                };
                resolve(fileInfo);
            });
        });
    }


});
var uploadFile = multer({storage: storage}).single("file");
var uploadFilesMiddleware = util.promisify(uploadFile);
module.exports = uploadFilesMiddleware;

Problem:

Deprecation Warning Listening to events on the Db class has been deprecated and will be removed in the next major version.

Fix:

So most probably the problem is not because of you. It is in the package itself. (check out the reference link above)

  1. Go to node_modules (it is that one folder with a lot of files) folder in your node_js application.

  2. Go to multer-gridfs-storage (inside node_modules)

  3. Go to lib folder (inside multer-gridfs-storage)

  4. Open gridfs.js

  5. Find this comment (// This are all the events that emit errors)

  6. Replace This

    this.db .on('error', errEvent) .on('parseError', errEvent) .on('timeout', errEvent) .on('close', errEvent);

With this

this.client
    .on('error', errEvent)
    .on('parseError', errEvent)
    .on('timeout', errEvent)
    .on('close', errEvent);

Basically change replace 'db' with 'client'.

You can also go to the official page and check out the current issues there it is clearly mentioned that multer-gridfs-storage has debrication waring issue.

Issue Link

koshur
  • 124
  • 1
  • 9