我遇到一个错误,错误消息为“错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头”。这是我的代码
router.post('/patent', upload.array('patentFiles', 2), (req, res) => {
var id = req.body.id
var patentFile = req.files
res.send(patentFile)
Patent.findOne({ id: id }, (err, doc) => {
if (doc) {
res.status(404).send('Already Exists!')
} else {
var insertPatentRecord = new Patent({
id: id
})
insertPatentRecord.save((err, doc) => {
if (err) {
res.send(err)
} else {
res.send({
msg: 'Saved',
doc: doc
})
}
})
}
})
})
非常感谢
答案 0 :(得分:0)
一旦将响应发送回客户端,对响应方法的任何进一步调用将导致“在将标头发送给客户端后无法设置标头”错误。为避免这种情况,请将return
放在响应调用之前。像这样:
return res.send(patentFile)
尽管查看响应的位置并发现您也在查询之后的专利集合,但您可能想要更改res.send(patentFile)
的位置。