【发布时间】:2018-02-02 19:06:54
【问题描述】:
我收到此错误:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):错误:发送后无法设置标头。
从我的快速路线路线的这一部分:
router.post('/', jsonParser, (req, res) => {
// checking that given id is valid
let { id } = req.body;
User.findById({ id })
.count()
.then(count => {
if (count < 1) {
return Promise.reject({
code: 422,
reason: 'Validation Error',
message: 'Family must be created by a user',
location: 'id'
})
}
return resolve();
})
.catch(err => {
return res.status(err.code).json({code: err.code, message: err.message, reason: err.reason, location: err.location })
});
...
我不擅长承诺。有人可以在这里看到我做错了什么吗?
【问题讨论】:
-
你已经在一个 promise 回调中。所以你应该使用 throw 而不是 reject。