获取 在 express js 中的 res.send() 之后发送标头后无法设置标头



每次执行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上看到了其他答案,但我已经应用了它们,但问题似乎并没有消失

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

根据这个代码库,你会得到错误,这就是为什么你的错误块调用外部响应

试试这个:

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

相关内容

最新更新