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

Event listener is not working while calling createConnection #11545

Closed
amustaque97 opened this issue Mar 20, 2022 · 2 comments
Closed

Event listener is not working while calling createConnection #11545

amustaque97 opened this issue Mar 20, 2022 · 2 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@amustaque97
Copy link

Do you want to request a feature or report a bug? BUG

What is the current behavior?
In current version of package, connection is not able to emit any event while calling createConnection mongoose function. This is happening because of asPromise that returns successful connection instance. Prior to version 6 it was working out of the box. I was able to listen on all connectionStates.

I'm not able to listen event createConnection as mentioned here: https://github.com/Automattic/mongoose/blob/master/lib/index.js#L283

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

  1. Create a new empty project. Type ( npm init -y)
  2. Install mongoose package, type npm install --save mongoose
  3. Create an index.js file and copy the below code and save that file.
const mongoose = require('mongoose');

(async() => {
    const conn = await mongoose.createConnection('mongodb://localhost:27017').asPromise();
    conn.on('connected', function(){ console.log('Hi')});
    conn.on('disconnected', function(){ console.log('Bye')});
})();
  1. Run this file and wait for few seconds. It will not console any of the above statements.

What is the expected behavior?
In an ideal scenario, it should print above statements based on the connectionState.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js Version: 16.13.2
Mongoose Version: 6.2.7
MongoDB Server Version: 5.0.6

Other comments
There is a workaround I can think of:

const mongoose = require('mongoose');

(async() => {
    const conn = mongoose.createConnection('mongodb://localhost:9972');
    conn.on('connected', function(){ console.log('Hi')});
    conn.on('disconnected', function(){ console.log('Bye')});
    conn.asPromise();
})();

The above code prints based on the state. I'm not sure if this is an intended feature or a bug. In my opinion it is a bug since we are not able to emit events.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 21, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

async function run() {
    const conn = await mongoose.createConnection('mongodb://localhost:27017').asPromise();
    console.log('ready state', conn.readyState);
    conn.on('connected', () => {
        console.log('connected')
    });
    console.log('done');
}

run();

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 28, 2022
@vkarpov15
Copy link
Collaborator

The problem is that const conn = await mongoose.createConnection('mongodb://localhost:27017').asPromise(); waits for Mongoose to finish connecting. So Mongoose is already connected when you register an event listener. The event listener works, you're just registering it too late.

Use the workaround you suggested instead if you need to use event listeners. Just make sure you add await before conn.asPromise():

(async() => {
    const conn = mongoose.createConnection('mongodb://localhost:9972');
    conn.on('connected', function(){ console.log('Hi')});
    conn.on('disconnected', function(){ console.log('Bye')});
    await conn.asPromise();
})();

However, in general, you don't need the event handlers you posted if you're already using async/await. You could do something like:

const mongoose = require('mongoose');

(async() => {
    const conn = await mongoose.createConnection('mongodb://localhost:27017').asPromise().then(conn => {
      console.log('Hi');
      return conn;
    });
    conn.on('disconnected', function(){ console.log('Bye')});
})();

If all you want to do is print a message when Mongoose's initial connection succeeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants