mongoose.connect never resolves since 4.11.2

Created on 13 Jul 2017  路  14Comments  路  Source: Automattic/mongoose

async function startDB() {
  const connectPromise = mongoose.connect(MONGOHQ_URL, {
    autoReconnect: true,
    reconnectTries: Number.MAX_VALUE,
    reconnectInterval: 1000,
    poolSize: 10,
    keepAlive: ms('30s'),
    connectTimeoutMS: ms('30s'),
    useMongoClient: true,
    promiseLibrary: global.Promise,
  });

  await connectPromise; // never resolves, even while open events is firing well
}

the code above works well in 4.11.1

All 14 comments

Same problem here. I was using mongoose.createConnection(), though.

Confirmed that these lines were being executed, and that promise in my case was an instanceof global.Promise (which is just the built-in, set by mongoose.Promise = global.Promise somewhere up the chain).

Dropping back to 4.11.1 works fine.

Can confirm the same happening here with .then() and .catch() not working after going from 4.11.1 to 4.11.2.

In the lines mentioned by @dvlsg why not just return the promise as is, why wrap them?

I'm having the same problem.

I pinned to 4.11.1 as opposed to getting 4.11.2 and it's working again. I've been using:

mongoose.Promise = global.Promise;
mongoose.connect(process.env.DATABASE, { useMongoClient: true })
  .then(() => {
    console.log('Connected to database');
  })
  .catch((err) => {
    console.log(`Error connecting to database: ${err}`);
  });

Mongoose documentation about useMongoClient can be found here.

@vkarpov15 ?

I have also been having this issue with 4.11.2. Like the posts above suggest, reverting back to 4.11.1 works fine.

@lahdekorpi I can't speak for sure, but I suspect the promise is not returned directly so that other Connection methods could be chained off of .connect() calls.

Coverage seems to be there for the change, so I'm a bit lost. Anyone try 4.11.2 _with_ useMongoClient: true? I didn't have that in our codebase yet.

edit: Nope, just tried it. Swing and a miss.

  await mongoose.connect(settings.url, {
            useMongoClient: true
        });

never resolves

i changed like this

   mongoose.connection.on('connected', function () {
            console.info(`${consolePrefix} ready`)
        });

        mongoose.connection.on('error', function (err) {
            console.error('MongoDB connection error: ', err);
        });

From my understanding, you actually need to return the actual Promise and not just pass the calls to .then and .catch? Or am I missing something here?

I had to use @p3x-robot's suggestion of using the connection events. Works now 馃憤

Seems to be fixed in 4.11.3

I can confirm that this issue is fixed in 4.11.3.

Duplicate of #5471, sorry for the trouble and thanks for bearing with us while we iron out the kinks in useMongoClient :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Igorpollo picture Igorpollo  路  3Comments

gustavomanolo picture gustavomanolo  路  3Comments

CodeurSauvage picture CodeurSauvage  路  3Comments

simonxca picture simonxca  路  3Comments

adamreisnz picture adamreisnz  路  3Comments