Documentation for Mongoose's on() method

2432 views mongodb

I can't see find any documentation for Mongoose's on() method.

const mongoose = require('mongoose');
var db = mongoose.connection;
db.on('open', () => {
   console.log('Connected to the MongoDB database.')
db.on('error', (err) => {
   console.log(`Database error: ${err}`);

In this sample code they provided 'open' for when the database connection is opened, and 'error' for connection errors, etc... I also see other examples providing 'connected' and 'disconnected' as parameters to the on() method. When would I use 'open' vs 'connected'? What other options exist besides these??? Where are these parameters defined? How does this on() method work exactly???

They're API docs give some cheap examples:

I know this might seem like a simple question, but its quite frustrating to not get clear definitions in an API spec.

answered question

1 Answer


Could it be a jQuery method?

It appears the event definitions are to be defined by the author of the jQuery object. The Mongoose docs loosely define a few of these events here:

But nowhere do they mention events such as 'open' and 'error'.

posted this

Have an answer?


Please login first before posting an answer.