發出帖子請求時,我收到此錯誤:
UnhandledPromiseRejectionWarning:未處理的承諾拒絕。
如果有人能解釋為什么會這樣,我將不勝感激:) 謝謝
router.post("/", async (req, res) => {
let client = new Client(req.body);
try {
savedClient = await client.save();
res.location(`/${savedClient._id}`).status(201).json(savedClient);
} catch {
res.status(500).json(savedClient.error);
}
});
uj5u.com熱心網友回復:
也許
let savedClient = await client.save();
uj5u.com熱心網友回復:
我認為應該是
...
} catch (error) {
res.status(500).json(savedClient.error);
}
因為 promise 的拒絕錯誤應該被 try/catch 陳述句捕獲。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312229.html
標籤:javascript 节点.js MongoDB 表达
