2

Essentially, my MongoDB connection closed in my Node.js app, right before a query was called, like this:

// connection dropped

yield Thing.findOneAndUpdate({ foo: 'bar' }, { foo: 'baz' })

The promise just hung there and never resolved, even after the connection was re-established (it didn't throw an error either).

I think my desired behavior here is for it throw an error when trying to run a command or query if there is no connection, so that's my question: how can I make it throw?

For one, I thought Mongoose would buffer commands and then execute them anyway when the connection is re-established, but that never happened either. According to this issue in the Mongoose GitHub, you can set bufferCommands: false and set the option on the connection bufferMaxEntries: 0 it would give me the desired "throw error if there is no connection during a command" behavior, but it didn't seem to do so either.

Any ideas?

Notes:

Using Mongoose v4.6.3 and Node v6.7.0, and my Mongoose config looks like this

{
  server: {
    poolSize: 20,
    socketOptions: {
      // 5 mins
      keepAlive: 300000,
      // 30 seconds
      connectTimeoutMS: 30000
    }
  },
  replset: {
    socketOptions: {
      keepAlive: 300000,
      connectTimeoutMS: 30000
    }
  }
}
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • Linked answer shows reproducible code with `bufferMaxEntries`. Your mongoose is notably quite a few revisions old, and I'm pretty sure that usage of `server` and `replSet` options separation has been deprecated in favor of top level options. – Neil Lunn Sep 19 '17 at 21:27

0 Answers0