Open In App

Mongoose Query.prototype.error() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.error() method of the Mongoose API is used on the Query objects. It allows us to get and set the error function and operation on particular query. That error operation will get executed before the exec() method finishes its operation. Let us understand error() method using an example.

Syntax:

query.error( err );

Parameters: This method accepts a single parameter as described below:

  • err: It is used to specify the error object.

Return Value: This method returns query object on which we can have a callback function.

Setting up Node.js Mongoose Module:

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: 

 

Database Structure: The database structure will look like this, the following database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection error() method. In this example, we are providing incorrect field name which is causing the error, and we are using the asynchronous function to handle the promise.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = connectionObject.model('Student', studentSchema);
  
(async () => {
    const result = await StudentModel.find(
        { num: "45221" })
        .error(new Error("Woop !!, no such field exists"));
    console.log(result);
})();


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

(node:18236) UnhandledPromiseRejectionWarning: Error: Woop !!, no such field exists
    at D:\GeeksforGeeks\Articles\Sakshi\Mongoose Query API .prototype.error()\app.js:21:64

Example 2: The below example illustrates the basic functionality of the Mongoose Connection error() method. In this example, we are not providing latest values to be updated to the update method which is causing the error, and we are using then and catch block to handle the promise.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = connectionObject.model('Student', studentSchema);
  
StudentModel
    .updateOne({ name: "45221" })
    .error(new Error("Please put the updated values")).then(res => {
        console.log(res);
    }).catch(err => console.log(err));


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Error: Please put the updated values

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-error



Last Updated : 20 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads