Skip to content

Commit

Permalink
fix(mongoose): fix .then() is not a function error when calling `mo…
Browse files Browse the repository at this point in the history
…ngoose.connect()` multiple times

Fix #9358
Fix #9335
Fix #9331
  • Loading branch information
vkarpov15 committed Aug 26, 2020
1 parent 0b8c6a1 commit fadc813
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/connection.js
Expand Up @@ -785,7 +785,7 @@ Connection.prototype.openUri = function(uri, options, callback) {
const promise = new Promise((resolve, reject) => {
const client = new mongodb.MongoClient(uri, options);
_this.client = client;
client.connect(function(error) {
client.connect((error) => {
if (error) {
_this.readyState = STATES.disconnected;
return reject(error);
Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Expand Up @@ -336,7 +336,15 @@ Mongoose.prototype.createConnection = function(uri, options, callback) {
Mongoose.prototype.connect = function(uri, options, callback) {
const _mongoose = this instanceof Mongoose ? this : mongoose;
const conn = _mongoose.connection;
return conn.openUri(uri, options, callback).then(() => _mongoose);

return promiseOrCallback(callback, cb => {
conn.openUri(uri, options, err => {
if (err != null) {
return cb(err);
}
return cb(null, _mongoose);
});
});
};

/**
Expand Down

0 comments on commit fadc813

Please sign in to comment.