【发布时间】:2018-05-06 19:26:56
【问题描述】:
我有路由器和两个从数据库中获取的 acync。在这里捕获错误的正确方法是什么?
router.get('/restaurant/:id', async (req, res, next) => {
var current_restaurant = await Restaurant.findOne({restaurantId: req.params.id}).exec();
var products = await Product.find({restaurant: req.params.id}).exec();
res.render('restaurant', {
user: req.user,
csrfToken: req.csrfToken(),
current_restaurant: current_restaurant,
products: products
});
});
【问题讨论】:
-
try/catch 是在使用 async/await 时捕获错误的方法 - 大多数体面的文档/教程都会显示这一点,例如 MDN code
-
我知道最后是投票,但是人们可以在接近投票的情况下表现出一点克制吗?我最近看到的很多问题都无缘无故地投票(或者至少不是他们选择的问题)
-
当然不是太宽泛,但考虑到所有使用 async/await 进行错误处理的示例,OP 至少可以证明他/她知道 try/catch。这使得这个问题没有得到很好的研究。如果该问题包含在问题中,则可能是重复的。
标签: javascript node.js express mongoose async-await