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

when mongoose.connect() returns an error the connection.readyState is set to 2 (connecting) #9921

Closed
fraleigh opened this issue Feb 11, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fraleigh
Copy link

fraleigh commented Feb 11, 2021

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

bug

What is the current behavior?

When mongoose.connect() throws an error, the mongoose.connection.readyState is sometimes 2 (connecting).

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

const mongoose = require('mongoose');

const dbUri = "mongodb+srv://username:password@badhost.rtiii.mongodb.net/Database"
const dbOptions = {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    retryWrites: true,
    w: 'majority'
};

mongoose.connect(dbUri, dbOptions)
    .then( () =>  {
        console.log("Connected");
    })
    .catch ((err) => {
        console.log(`connect error: ${err.message}`);
        if (mongoose.connection.readyState != 0) {
            console.log(`mongoose.connect returned an error, but ready state is ${mongoose.connection.readyState}`);
        }
    })

Once every 2-3 runs, the following logs are printed

connect error: querySrv ENOTFOUND _mongodb._tcp.badhost.rtiii.mongodb.net
mongoose.connect returned an error, but ready state is 2

The correct runs, just print

connect error: querySrv ENOTFOUND _mongodb._tcp.badhost.rtiii.mongodb.net

What is the expected behavior?

I would expect the ready state to be 0 (disconnected) if mongoose.connect() fails.

This may be related to #8692. The error does not happen with useUnifiedTopology false. However, in the case where mongoose.connect() fails I would expect the readyState to be appropriately set, not to be delayed by the serverSelectionTimeoutMS.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node 14.15.4
Mongoose 5.11.13
MongoDB - Atlas 4.4.3
I am running the code on OS X 11.2.

@vkarpov15 vkarpov15 modified the milestones: 5.11.17, 5.11.18 Feb 15, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed aware labels Feb 23, 2021
This was referenced Mar 5, 2021
This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants