Mongoose Query.prototype.catch() API
Last Updated :
20 Jan, 2023
The Mongoose Query API.prototype.catch() method of the Mongoose API is used on the Query objects. It allows us to execute the query returned promise. Using this method we can handle the rejected promise error and can display it or use it for next processes. Rejected handler by the promise can be handled using catch() block or method. Let us understand the catch() method using an example.
Syntax:
promiseObject.catch( reject );
Parameters: This method accepts a single parameter as described below:
- reject: It is used to specify rejected promise handler.
Return Value: This method returns promise which can be handled using 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 catch() method. In this example, promise has been resolved that is why the code execution has not entered in catch block and we are getting all the documents from the collection.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
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.find().then(res => {
console.log(res);
}). catch (error => {
console.log( 'Error' , error)
});
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
[
{
_id: new ObjectId("63c2fe2ef9e908eb17f225da"),
name: 'Student1',
age: 25,
rollNumber: 36,
__v: 0
},
{
_id: new ObjectId("63c2fe2ef9e908eb17f225db"),
name: 'Student2',
age: 18,
rollNumber: 65,
__v: 0
},
{
_id: new ObjectId("63c2fe2ef9e908eb17f225dc"),
name: 'Student3',
age: 15,
rollNumber: 36,
__v: 0
}
]
Example 2: The below example illustrates the basic functionality of the Mongoose Connection catch() method. In this example, promise has been rejected that is why the code execution has entered in the catch block and we are getting the expected error with error message.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
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.update(
{ name: "Student1" },
{ age: 'Eight' }
).then(res => {
console.log(res);
}). catch (error => {
console.log( 'Error' , error)
});
|
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 CastError: Cast to Number failed for value "Eight" (type string) at path "age"
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-catch
Share your thoughts in the comments
Please Login to comment...