【发布时间】:2020-05-18 12:44:32
【问题描述】:
我正在开发一个可以将目的地保存到我的 Mongo DB 的应用程序。在尝试保存数据库中已经存在的目标时,我想抛出一个自定义错误。 Mongoose 可以防止这种情况发生,但我想要清晰且用户友好的错误处理。
// post a new destination
router.post('/',
(req, res) => {
const newCity = new cityModel(
{
name: req.body.name,
country: req.body.country
}
)
newCity.save()
.then(city => {
res.send(city)
})
.catch(err => {
res.status(500).send('Server error')
})
});
【问题讨论】:
标签: javascript mongodb mongoose ecmascript-6 error-handling