【发布时间】:2018-08-22 17:34:26
【问题描述】:
当我尝试将数据保存到 MngoDB 时出现以下错误。 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:481:11)
它工作正常,我不知道这个错误来自哪里!我已尝试按照建议安装和卸载 jest,但没有用。
这是错误发生的时间:
dataTrattamenti.save((error) => {
if (error) {
res.send({success: false, message: 'We cannot save the data, there is an error: ', error})
} else
res.send({success: true, message: 'Successfully saved'})
});
res.end({success: true, message: 'Successfully saved'})
正如一些人所建议的,我在 Node 中没有收到任何错误,但它发送的是
'We cannot save the data, there is an error:
虽然错误变量似乎为空。 我用邮递员尝试了代码,它工作正常。请帮忙,我不知道发生了什么。错误的标题是什么?
【问题讨论】:
-
该错误意味着您已经在代码中的某处调用了“res.send”。那么你就不能再调用“send”了。
-
对于那个 post 函数,我有两个 res.send ,它们在 if 的正文中。我想知道为什么它突然开始捕捉到这个错误。
标签: node.js mongodb express mongoose-schema