Dean Ambrose
Dean Ambrose

Reputation: 193

Getting Can't set headers after they are sent after res.send() in express js

I am getting this error every time a do a POST request. My code is:

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})
    })
}

)

I get this error every time irrespective of which res.status() gets called depending on the condition.

I have seen other answers on SO but and I have applied them but the problem doesn't seem to go away.

Upvotes: 0

Views: 35

Answers (1)

abdulbari
abdulbari

Reputation: 6232

You are sending response two times that's why getting this error.

And as per this code base you are getting error that's why your error block is calling with outer response

Try this:

    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
      });
    })
  });

Upvotes: 1

Related Questions