Documentation for Mongoose's on() method

2432 views mongodb
-1

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

const mongoose = require('mongoose');
mongoose.connect(MONGODB_URI);
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: https://mongoosejs.com/docs/api.html

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

2

Could it be a jQuery method?

https://www.w3schools.com/jquery/event_on.asp

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:

https://mongoosejs.com/docs/api.html#connection_Connection-readyState

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

posted this

Have an answer?

JD

Please login first before posting an answer.