【问题标题】:Getting Can't set headers after they are sent after res.send() in express js在 express js 中的 res.send() 之后发送后无法设置标头
【发布时间】:2019-01-14 03:04:32
【问题描述】:

每次执行POST 请求时,我都会收到此错误。 我的代码是:

records.post('/addNewRecord',
[
    check('applicant_name').not().isEmpty().withMessage('Applicant name cannot be empty').trim().escape(),
    check('applicant_type').not().isEmpty().withMessage('Please select an applicant type').trim().escape(),
    check('applicant_address').not().isEmpty().withMessage('Applicant address cannot be empty').trim().escape(),
    check('applicant_contact').not().isEmpty().withMessage('Applicant contact cannot be empty').trim().escape(),
    check('building_name').not().isEmpty().withMessage('Building name cannot be empty').trim().escape(),
    check('building_address').not().isEmpty().withMessage('Building Address cannot be empty').trim().escape(),
    check('building_area').not().isEmpty().withMessage('Building Area cannot be empty').trim().escape(),
    check('file_number').not().isEmpty().withMessage('File number cannot be empty').trim().escape(),
    check('remark').trim().escape()
],
function(req, res) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        res.status(200).send({message: errors.array(), saved : false})
    }
    const fileRecordData = {
        "APPLICANT_NAME": req.body.applicant_name,
        "APPLICANT_TYPE": req.body.applicant_type,
        "APPLICANT_ADDRESS": req.body.applicant_address,
        "APPLICANT_CONTACT": req.body.applicant_contact,
        "BUILDING_NAME": req.body.building_name,
        "BUILDING_ADDRESS": req.body.building_address,
        "BUILDING_AREA": req.body.building_area,
        "FILE_NUMBER": req.body.file_number,
        "REMARK": req.body.remark,
    }
    connection.query(`INSERT INTO ${process.env.FILE_RECORD_TBL} SET ?`, fileRecordData, function(err, results, fields) {
        if (err) {
            res.status(200).send({message : err, saved : false})
        }
        res.status(200).send({message : 'Record saved successfully', saved : true})
    })
}

)

无论根据条件调用哪个 res.status(),我每次都会收到此错误。

我在 SO 上看到了其他答案,但我已经应用了它们,但问题似乎并没有消失。

【问题讨论】:

  • 你能发布完整的路线吗?也许其他代码首先发送一些东西。发布应用 js(至少相关部分)
  • @AritraChakraborty 更新了完整路线的问题,请检查

标签: node.js express


【解决方案1】:

您发送了两次响应,这就是收到此错误的原因。

根据此代码库,您会收到错误,这就是您的错误块使用外部响应调用的原因

试试这个:

    records.post('/addNewRecord', [
    check('applicant_name').not().isEmpty().withMessage('Applicant name cannot be empty').trim().escape(),
    check('applicant_type').not().isEmpty().withMessage('Please select an applicant type').trim().escape(),
    check('applicant_address').not().isEmpty().withMessage('Applicant address cannot be empty').trim().escape(),
    check('applicant_contact').not().isEmpty().withMessage('Applicant contact cannot be empty').trim().escape(),
    check('building_name').not().isEmpty().withMessage('Building name cannot be empty').trim().escape(),
    check('building_address').not().isEmpty().withMessage('Building Address cannot be empty').trim().escape(),
    check('building_area').not().isEmpty().withMessage('Building Area cannot be empty').trim().escape(),
    check('file_number').not().isEmpty().withMessage('File number cannot be empty').trim().escape(),
    check('remark').trim().escape()
  ],
  function (req, res) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        // break the function execution if condition is true
     return  res.status(200).send({
        message: errors.array(),
        saved: false
      });
    }
    const fileRecordData = {
      "APPLICANT_NAME": req.body.applicant_name,
      "APPLICANT_TYPE": req.body.applicant_type,
      "APPLICANT_ADDRESS": req.body.applicant_address,
      "APPLICANT_CONTACT": req.body.applicant_contact,
      "BUILDING_NAME": req.body.building_name,
      "BUILDING_ADDRESS": req.body.building_address,
      "BUILDING_AREA": req.body.building_area,
      "FILE_NUMBER": req.body.file_number,
      "REMARK": req.body.remark,
    }
    connection.query(`INSERT INTO ${process.env.FILE_RECORD_TBL} SET ?`, fileRecordData, function (err, results, fields) {
      if (err) {
          // break the callback execution if getting error
        return res.status(200).send({
          message: err,
          saved: false
        })
      }
      res.status(200).send({
        message: 'Record saved successfully',
        saved: true
      });
    })
  });

【讨论】:

  • 这适用于返回,但使用 res.send() 强制返回??
  • 如何正确处理它?....你答案的第一部分对我也不起作用
  • 主要目的是它不应该发送超过一次的响应,现在取决于你想要如何处理
  • 你的意思是即使它们在 if-else 块中,它们也应该在一条路线中只发送一次?........你能告诉我如何在不返回的情况下做到这一点
  • 我已经更新了答案,它应该适用于所有场景,顺便说一句,您需要注意执行的控制流程
猜你喜欢
  • 2015-12-25
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2017-08-11
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多