Mongoose Query.prototype.catch()函数

Mongoose Query.prototype.catch()函数

Mongoose 的 Query API.prototype.catch() 方法 用于查询对象上。它允许我们执行查询返回的 promise。使用此方法,我们可以处理rejected promise错误,并显示它或在下一步流程中使用它。通过promise的rejected处理程序可以使用catch()块或方法处理。让我们通过一个示例来理解catch()方法。

语法:

promiseObject.catch( reject );
JavaScript

参数: 该方法接受一个描述如下的参数:

  • reject: 用于指定拒绝的 promise 处理程序。

返回值: 该方法返回一个可以使用回调函数处理的 promise。

设置 Node.js Mongoose 模块:

步骤1: 使用以下命令创建一个 Node.js 应用程序:

npm init
JavaScript

步骤2: 创建NodeJS应用程序后,使用以下命令安装所需的模块:

npm install mongoose
JavaScript

项目结构:

项目结构将如下所示:

Mongoose Query.prototype.catch()函数

数据库结构: 数据库结构将会像这样,在MongoDB中存在以下数据库。

Mongoose Query.prototype.catch()函数

示例1: 下面的示例演示了Mongoose Connection catch()方法的基本功能。在这个示例中,promise已经被解析,所以代码执行没有进入catch块,并且我们从集合中获取了所有的文档。

文件名:app.js

// Require mongoose module 
const mongoose = require("mongoose"); 
  
// Set Up the Database connection 
const URI = "mongodb://localhost:27017/geeksforgeeks"; 
  
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) 
});
JavaScript

运行程序的步骤: 要运行应用程序,请从项目的根目录执行以下命令:

node app.js
JavaScript

输出:

[
  {
    _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
  }
]
JavaScript

示例2: 下面的示例演示了Mongoose Connection catch() 方法的基本功能。在这个示例中,promise被拒绝,这就是为什么代码执行进入catch块,并且我们得到了带有错误消息的预期错误。

文件名:app.js

// Require mongoose module 
const mongoose = require("mongoose"); 
  
// Set Up the Database connection 
const URI = "mongodb://localhost:27017/geeksforgeeks"; 
  
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) 
});
JavaScript

运行程序的步骤: 从项目的根目录执行以下命令来运行应用程序:

node app.js
JavaScript

输出:

Error CastError: Cast to Number failed for value "Eight" (type string) at path "age"
JavaScript

参考: https://mongoosejs.com/docs/api/query.html#query_Query-catch

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册