我有一种使用mongoose将产品发布到mongodb中的方法。我正在使用async await而不是then-catch块。我的代码:
const Category = require('../models/category');
exports.postProduct = async (req,res,next)=>{
//const userId = req.user.userId;
const userId = '5dca886a1ee97b07002de048';
// const category = req.body.category;
const category = ['bikes','cars'];
// const tags = req.body.tags;
const tags = ['wheels','vehicles','travel','s','s','s'];
//const imageUrl = req.body.imageUrl;
const imageUrl = ['https://picsum.photos/200/300','https://picsum.photos/200/300','https://picsum.photos/200/300'];
try {
if(tags.length>5){
const error = new Error('Select only 5 Tags');
error.statusCode = 406;
throw error;
}
if (!category || category === []){
const error = new Error('Selected category is empty, select category again');
error.statusCode = 406;
throw error;
}
const categoryFound = await Category.find({name: {$in:category}}).select('name');
if (categoryFound) {
const addProduct = new Product(
{ name : 'vehicle',
description:'Its a good vehicle',
categoryId: categoryFound,
productImageUrl: imageUrl,
creatorId:userId,
productRequirement:'buy',
tags:tags
});
const productSaved = await addProduct.save();
res.status(200).json({message:productSaved});
}else{
const error = new Error('Category not found!');
error.statusCode = 404;
throw error;
}
} catch (err) {
if (!err.statusCode) {
err.statusCode = 500;
}
next(err);
}
};
捕获中的错误由我的app.js中的快速中间件捕获。
//Error handling middleware
app.use((error, req, res, next) => {
const status = error.statusCode || 500;
const message = error.message;
res.status(status).json({ message: message, status: status });
});
这很好。在这种特定情况下,当数组“标签”长于5时,我来自Postman(REST API开发工具)的请求将返回:
{
"message": "Select only 5 Tags",
"status": 406
}
当我尝试在try-catch之外使用'if'检查时,出现此错误:
UnhandledPromiseRejectionWarning: Error: Select only 5 Tags
at exports.postProduct (F:\project\controllers\products.js:17:23)
at Layer.handle [as handle_request] (F:\project\node_modules\express\lib\router\layer.js:95:5)
at next (F:\project\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\project\node_modules\express\lib\router\layer.js:95:5)
at F:\project\node_modules\express\lib\router\index.js:281:22
at Function.process_params (F:\project\node_modules\express\lib\router\index.js:335:12)
at next (F:\project\node_modules\express\lib\router\index.js:275:10)
at Function.handle (F:\project\node_modules\express\lib\router\index.js:174:3)
at router (F:\project\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (F:\project\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (F:\project\node_modules\express\lib\router\index.js:317:13)
at F:\project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (F:\project\node_modules\express\lib\router\index.js:335:12)
at next (F:\project\node_modules\express\lib\router\index.js:275:10)
at F:\project\app.js:54:5
这种在try-catch内抛出错误以检查长度,使用if语句有效检查空数组的方式吗?有什么更好的方法吗?
答案 0 :(得分:0)
那不是很明显吗?您正在抛出一个错误,但是没有人可以抓住它。创建async
函数时,它返回一个Promise。因此,当您抛出错误但没有捕获错误时,该函数将返回被拒绝的promise。这就是为什么您得到UnhandledPromiseRejectionWarning
的原因。使用异步/等待时,您必须使用try-catch捕获承诺拒绝。