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

Error opening an already open connection #9331

Closed
michaeldacey opened this issue Aug 15, 2020 · 1 comment
Closed

Error opening an already open connection #9331

michaeldacey opened this issue Aug 15, 2020 · 1 comment
Milestone

Comments

@michaeldacey
Copy link

Using the current versions of Mongoose and TypeScript @types/express.

Opening a connection fails to return the Promise if the connection is already open.
Here is the my function:

async MongodbConnection(dbname: string): Promise
{
try
{
await mongoose.connect(mongodb://mongodb:27017/${dbname}?authSource=admin, {
'useNewUrlParser': true,
'useUnifiedTopology': true,
'useFindAndModify': false,
'useCreateIndex': true,
'user': 'docker',
'pass': 'password'
});
}
catch(error)
{
throw error;
}
return mongoose.connection;
}

To fix this either close the connection before calling this function again or add a check to this function to ensure the connection is not open.

if(mongoose.connection.readyState !== 1)
{
await mongoose.connect(mongodb://webservice:27017/${dbname}?authSource=admin, {
'useNewUrlParser': true,
'useUnifiedTopology': true,
'useFindAndModify': false,
'useCreateIndex': true,
'user': 'docker',
'pass': 'password'
});
}

I would expect await mongoose.connect(...) to perform this check. Either it could throw an exception or close the existing connection then reopen it again.

@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 25, 2020
@vkarpov15
Copy link
Collaborator

Sorry for the trouble, we'll ship a fix for this in v5.10.1 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants