Skip to content

Commit 7d29e77

Browse files
committed
fix(connection): set readyState to disconnected if initial connection fails
Fix #6244
1 parent 8592280 commit 7d29e77

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

lib/connection.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Connection.prototype.openUri = function(uri, options, callback) {
337337
const promise = new Promise((resolve, reject) => {
338338
parser(uri, options, (error, parsed) => {
339339
if (error) {
340+
this.readyState = STATES.disconnected;
340341
if (_this.listeners('error').length > 0) {
341342
_this.emit('error', error);
342343
}
@@ -357,6 +358,7 @@ Connection.prototype.openUri = function(uri, options, callback) {
357358

358359
mongodb.MongoClient.connect(uri, options, function(error, client) {
359360
if (error) {
361+
_this.readyState = STATES.disconnected;
360362
if (_this.listeners('error').length > 0) {
361363
_this.emit('error', error);
362364
}
@@ -366,13 +368,6 @@ Connection.prototype.openUri = function(uri, options, callback) {
366368
_this.db = db;
367369
_this.client = client;
368370

369-
if (error) {
370-
_this.readyState = STATES.disconnected;
371-
if (_this.listeners('error').length) {
372-
_this.emit('error', error);
373-
}
374-
return reject(error);
375-
}
376371
// Backwards compat for mongoose 4.x
377372
db.on('reconnect', function() {
378373
_this.readyState = STATES.connected;

0 commit comments

Comments
 (0)