每次执行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上看到了其他答案,但是我已经应用了它们,但是问题似乎并没有消失。
答案 0 :(得分: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
});
})
});