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

Created on 16 Aug 2020  路  7Comments  路  Source: Automattic/mongoose

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.

Most helpful comment

Sorry for the trouble, fix will be in 5.10.1 :+1:

All 7 comments

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

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.

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

https://github.com/Automattic/mongoose/commit/751680e3f1d9751da7a90c72929aeeab30e383b0

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

https://github.com/Automattic/mongoose/blob/25cdb958aa92e5fe2e998cb437393dbca5c613ca/lib/connection.js#L662-L673

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.

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 (https://github.com/Automattic/mongoose/issues/9151) explaining that thing with closing connections with this new version of mongoose.

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 https://github.com/Automattic/mongoose/issues/8810

Sorry for the trouble, fix will be in 5.10.1 :+1:

Was this page helpful?
0 / 5 - 0 ratings