Mongoose Document Model.events API
The Model.events property of Mongoose API is used to return the model’s events instance. The instance of the event contains the fields like _events, _eventsCount, and _maxListeners.
Syntax:
Model_Name.events
Return Value: The Model.events property returns the events instance of the model.
Setting up Node.js application:
Step 1: Create a Node.js application using the following command:
npm init
Step 2: After creating the NodeJS application, Install the required module using the following command:
npm install mongoose
Project Structure: The project structure will look like this:
Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using the events property on the User model which will return the events instance of the model.
- app.js: Write down the below code in the app.js file:
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) const userSchema = new mongoose.Schema( { name: String, age: Number } ) // Defining userSchema model const User = mongoose.model( 'User' , userSchema); // Getting events property const output = User.events console.log( "Events instance is:" ,output) |
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
Events instance is: EventEmitter { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, [Symbol(kCapture)]: false }
Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “father_name”, and “mother_name”. In the end, we are using events property on the Student model which will return events instance of the model with an event on it.
- app.js: Write down the below code in the app.js file:
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) const studentSchema = new mongoose.Schema( { name: String, father_name: String, mother_name: String } ) // Defining studentSchema model const Student = mongoose.model( 'Student' , studentSchema); // Setting the events property Student.events.on( 'error' , err => console.log(err.message)); // Accessing base property on Student model const output = Student.events console.log( "Events Instance is:" , output) |
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
Events Instance is: EventEmitter { _events: [Object: null prototype] { error: [Function (anonymous)] }, _eventsCount: 1, _maxListeners: undefined, [Symbol(kCapture)]: false }
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-events
Please Login to comment...