Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version #9930

Closed
Systerr opened this issue Feb 14, 2021 · 13 comments

Comments

@Systerr
Copy link

Systerr commented Feb 14, 2021

Do you want to request a feature or report a bug?
bug (deprecation warning)

What is the current behavior?
On start we have deprecation warning into console

If the current behavior is a bug, please provide the steps to reproduce.

(node:9455) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
    at _setClient (node_modules/mongoose/lib/connection.js:943:6)
    at node_modules/mongoose/lib/connection.js:839:7
    at node_modules/mongodb/lib/utils.js:693:5
    at node_modules/mongodb/lib/mongo_client.js:227:7
    at connectCallback (node_modules/mongodb/lib/operations/connect.js:366:5)
    at node_modules/mongodb/lib/operations/connect.js:602:5
    at Object.connectHandler [as callback] (/node_modules/mongodb/lib/core/sdam/topology.js:297:43)
    at processWaitQueue (node_modules/mongodb/lib/core/sdam/topology.js:1048:21)
    at NativeTopology.serverUpdateHandler (node_modules/mongodb/lib/core/sdam/topology.js:567:7)
    at Server.emit (events.js:315:20)

What is the expected behavior?
No deprecation warning
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Latest 5.11.16

I digged in into warning and found why this happens:

  1. This introduced into mongo driver on version v3.6.4 refactor: deprecate Db.on and Db.once mongodb/node-mongodb-native#2610 as part on preparation of driver v4 release
  2. Latest mongoose 5.11.16 introduced mongo drive update Upgrade mongodb driver to 3.6.4 #9893
  3. Original change happens because of refactor(db)!: suppress Db events mongodb/node-mongodb-native#2251
@Systerr
Copy link
Author

Systerr commented Feb 14, 2021

@pnutmath
Copy link
Contributor

https://jira.mongodb.org/browse/NODE-1709?focusedCommentId=2962047&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2962047

Only MongoClient will receive events not DB class in future

@purpshell
Copy link

how does this get fixed?

@vkarpov15 vkarpov15 added this to the 5.11.18 milestone Feb 18, 2021
@vkarpov15 vkarpov15 added the developer-experience This issue improves error messages, debugging, or reporting label Feb 18, 2021
@lantw44
Copy link
Contributor

lantw44 commented Feb 24, 2021

The warning still exists in mongoose 5.11.18:

(node:90089) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
    at _setClient (node_modules/mongoose/lib/connection.js:932:10)
    at node_modules/mongoose/lib/connection.js:838:7
    at node_modules/mongodb/lib/utils.js:693:5
    at node_modules/mongodb/lib/mongo_client.js:227:7
    at connectCallback (node_modules/mongodb/lib/operations/connect.js:366:5)
    at node_modules/mongodb/lib/operations/connect.js:602:5
    at connectHandler (node_modules/mongodb/lib/core/sdam/topology.js:297:43)
    at cb (node_modules/mongodb/lib/core/sdam/topology.js:681:26)
    at node_modules/mongodb/lib/cmap/connection_pool.js:350:13
    at handleOperationResult (node_modules/mongodb/lib/core/sdam/server.js:558:5)
    at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:277:5)
    at MessageStream.emit (events.js:315:20)
    at MessageStream.EventEmitter.emit (domain.js:529:15)
    at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)

@troykelly
Copy link

@vkarpov15 can this please be reopened until it's confirmed resolved?

@vkarpov15 vkarpov15 reopened this Mar 1, 2021
@vkarpov15
Copy link
Collaborator

@lantw44 @troykelly do you have useUnifiedTopology: true set?

@vkarpov15 vkarpov15 modified the milestones: 5.11.18, 5.11.19 Mar 1, 2021
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed developer-experience This issue improves error messages, debugging, or reporting labels Mar 1, 2021
@lantw44
Copy link
Contributor

lantw44 commented Mar 2, 2021

@lantw44 @troykelly do you have useUnifiedTopology: true set?

Yes, useUnifiedTopology: true is set when calling mongoose.createConnection.

@vkarpov15
Copy link
Collaborator

I'm unable to repro, the below script doesn't print any warnings for me. Can you please modify the below script to demonstrate your issue?

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.createConnection('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  console.log('Done');
}

@vkarpov15 vkarpov15 removed this from the 5.11.19 milestone Mar 4, 2021
@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Mar 4, 2021
@FlorentinTh
Copy link

FlorentinTh commented Mar 4, 2021

@vkarpov15 just replacing your connection string from localhost to an instance of MongoDB Atlas in your script fires the warning like so :

mongodb+srv://<username>:<password>@cluster-ign8c.mongodb.net/test?retryWrites=true&w=majority

So maybe the issue is not related to Mongoose ?!

@Unknow1002
Copy link

Unknow1002 commented Mar 4, 2021

@vkarpov15 vkarpov15 modified the milestones: 5.12.1, 5.12.2, 5.12.3 Mar 18, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.12.3, 5.12.4 Mar 31, 2021
@tukusejssirs
Copy link

tukusejssirs commented Apr 8, 2021

This happens to me connecting to new database (const newDb = await mongoose.connection.useDb('newDb')), while I am already connected to a different database; see #8267.

Note: I am connected to a local, fresh database, therefore it is not only connected to Mongo Altas databases.

@vkarpov15
Copy link
Collaborator

@tukusejssirs the below script doesn't print any warnings. Please modify the below script to demonstrate your issue:

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.createConnection('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });


  const newDb = await mongoose.connection.useDb('newDb');

  console.log('Done');
}

@FlorentinTh I tried with my Atlas cluster and I don't get any warning:

  await mongoose.createConnection('mongodb+srv://OMITTED:[email protected]/myFirstDatabase?retryWrites=true&w=majority', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

I'm going to close this issue. If you're still seeing this warning:

  1. Please make sure you've upgraded to the latest version of Mongoose, v5.12.3 as of this writing.
  2. If that doesn't fix the issue, please open a new issue and follow the issue template.

@vkarpov15 vkarpov15 removed this from the 5.12.4 milestone Apr 13, 2021
@Automattic Automattic locked and limited conversation to collaborators Apr 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants