Andy Brunner
Andy Brunner

Reputation: 173

Does MongoClient.connect emitt anything?

Does the MongoClient.connect function emitt any events, e.g. db.on('error')? I could not find anything in the 'mongodb' driver documentation.

In my application, I should monitor the connection and write a warning messages to a log whenever the connection is lost, even if I have "autoReconnect:true" active.

Upvotes: 0

Views: 52

Answers (1)

evilive
evilive

Reputation: 1879

MongoClient.connect returns promise or uses callback. So it does not emit anything. But the object Db which you receive when the method resolved inherits EventEmitter. So you can listen it for this events

MongoClient.connect(url, options, function(err, db){
  db.on('error', /* log error */ )
})

Upvotes: 1

Related Questions