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

TypeError: conn.openUri(...).then is not a function #9335

Closed
jb33k opened this issue Aug 16, 2020 · 7 comments
Closed

TypeError: conn.openUri(...).then is not a function #9335

jb33k opened this issue Aug 16, 2020 · 7 comments
Milestone

Comments

@jb33k
Copy link

jb33k commented Aug 16, 2020

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

What is the current behavior?
I'm using the following to connect to MongoDB via Mongoose:

 conn = Mongoose.connect(conn_str, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      family: 4,
    }).catch((error) => {
      throw error
    })

Once I run the script with around 20-100 connections, I start getting the following error:

TypeError: conn.openUri(...).then is not a function
at Mongoose.connect (/src/node_modules/mongoose/lib/index.js:339:47)

I can't figure out what is causing this.

If the current behavior is a bug, please provide the steps to reproduce.
Look at the above. I can't replicate this reliably because it occurs only when there is a lot of connections and data flying.

What is the expected behavior?
There wouldn't be an error.

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

@Vexyr
Copy link

Vexyr commented Aug 16, 2020

While switching to Mongoose 5.10, I had the same problem.

@jb33k
Copy link
Author

jb33k commented Aug 17, 2020

While switching to Mongoose 5.10, I had the same problem.

Ok, good to know it's not just me. Did you find a way to fix it, except downgrading? 5.10 seems to be the latest.

@mryellow
Copy link

mryellow commented Aug 17, 2020

Looking through the history there is this commit attempting to fix a re-connection/multi-connection issue.

751680e

It introduces a return this which at a glance looks to happen before promises are setup.

mongoose/lib/connection.js

Lines 662 to 673 in 25cdb95

if (this.readyState === STATES.connecting || this.readyState === STATES.connected) {
if (this._connectionString !== uri) {
throw new MongooseError('Can\'t call `openUri()` on an active connection with ' +
'different connection strings. Make sure you aren\'t calling `mongoose.connect()` ' +
'multiple times. See: https://mongoosejs.com/docs/connections.html#multiple_connections');
}
if (typeof callback === 'function') {
callback(null, this);
}
return this;
}

@mryellow
Copy link

Using createConnection may be a work-around for some. However others will be waiting for an existing pending connection and not desire creating a new one.

connect() should probably store the promise and then return that same pending promise on subsequent calls.

@Beatricce
Copy link

Have you tried closing the connection after doing what you have to do with the data? I was having the same bug but after a reaaaaally long time I found this issue (#9151) explaining that thing with closing connections with this new version of mongoose.

@clhuang
Copy link
Contributor

clhuang commented Aug 25, 2020

it seems to be that once the connection is set up, the .then and .catch methods that made it into a promise get removed: https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L937

related to #8810

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

Sorry for the trouble, fix will be in 5.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

Successfully merging a pull request may close this issue.

6 participants