gpt4 book ai didi

javascript - 在异步等待中的try-catch中处理 'throw error'是否有问题?

转载 作者:行者123 更新时间:2023-12-03 08:44:04 25 4
gpt4 key购买 nike

我有一种使用 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语句检查空数组的方法有效吗?有什么更好的方法吗?

最佳答案

那不是很明显吗?您正在抛出一个错误,但是没有人可以捕获它。创建async函数时,它会返回一个promise。因此,当您抛出错误但没有捕获错误时,该函数将返回被拒绝的promise。这就是为什么您得到UnhandledPromiseRejectionWarning的原因。当您使用异步/等待时,必须使用try-catch捕获 promise 拒绝。

关于javascript - 在异步等待中的try-catch中处理 'throw error'是否有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com