I am connecting a nodejs app to my mongodb database using the following:
const url = 'mongodb://localhost:27017/?replicaSet=rs'
let client = new MongoClient(url, {
useNewUrlParser: true,
connectTimeoutMS: 60000,
socketTimeoutMS: 60000,
})
try {
let dbclient = await client.connect()
console.log(dbclient)
const db = dbclient.db('test')
const collection = db.collection('accounts')
const changeStream = collection.watch(pipeline)
changeStream.on("change", function(change) {
console.log('changed', change)
})
} catch (err) {
console.log('mongo err:', err)
}
This works perfectly, however it regularly loses connection after a few minutes with the error:
Uncaught MongoNetworkError: connection 6 to localhost:27017 timed out
According to the documentation it should automatically reconnect up to 30 tries upon error, however it does not appear to make any further attempts to reconnect.
I will also need to run some additional logic on reconnect to properly handle local state.
How can I catch and handle these errors?
Edit: While I'm still not getting other events, I am getting 'reconnect' events after the errors occur. So it seems I can at least react to the errors, but still not actually catch them.