【发布时间】:2016-09-01 21:52:57
【问题描述】:
我有一个 NodeJS Rest API,其中有一个用户集合,此外我还进行用户 SMS 验证。
这是 POST /:id/verification 的控制器
exports.verification = (req, res) => {
const id = req.params.id
return User.find(id)
.then( user => {
if (user.code !== req.body.code) {
res.json({ message: 'Incorrect code' })
res.sendStatus(500)
return
}
user.isVerified = true
user.save( error => {
if (error) {
res.json({ message: 'Failed to update user' })
res.sendStatus(500)
return
}
res.json({ user })
res.sendStatus(200)
} )
} )
.catch( error => {
res.json({ error })
} )
}
但问题是,当我发布到 /:id/verification 时,我收到了这个错误
错误:发送后无法设置标头。 - NodeJS 和 Express
在这一行:
res.json({ user })
res.sendStatus(200)
但我不明白为什么,在此之前我没有发送任何回复。
谁能解释我做错了什么?
【问题讨论】: