![](/img/trans.png)
[英]I have catch() in promise chain but still gets unhandled promise rejection
[英]I am getting Unhandled Promise Rejection error even though I have the catch section in my code
我只是想處理來自發布請求的數據。 我正在處理catch部分中的數據,以便將其記錄在控制台中,但是仍然出現錯誤Unhandled Promise Rejection。
router.post('/', (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
});
.catch(err => console.log(err));
res.status(201).json({
message: 'Handling post request to /products.',
createdProduct: product
});
});
有時數據庫庫會這樣做。 我以前也有類似的經歷。 您可以使用全局錯誤處理程序,看看發生了什么。
process.on('unhandledRejection', function(reason, p){
//call handler here
});
這可能會為這個問題提供更多啟示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.