【问题标题】:Error while saving in MongoDB在 MongoDB 中保存时出错
【发布时间】: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


【解决方案1】:

所以,我已经单独记录了错误消息,因为在下面的代码中,它不会记录任何错误,我得到的是,这条消息只有We cannot save the data, there is an error:

res.send({success: false, message: 'We cannot save the data, there is an error: ', error})
然后,我看到错误不是关于标题,而是数据库中的重复错误。所以我解决了。

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多