As a beginner, I have encountered an error message stating that the API is trying to set the response more than once. I am aware of the asynchronous nature of Node.js but I am struggling to debug this issue. Any assistance would be greatly appreciated.
router.route("/new").post((req, res) => {
//Variable declarations
sql.open(connectionString, function(err, conn){
var pm = conn.procedureMgr();
pm.callproc('dbo.AddRequestDetails',[request_zone,request_type,requester_type,request_flow,
sales_order,requester_email,quotation,request_reason,request_status,
StatusFlag, bot_status,bot_comments,additional_notification_to,additional_information, approver,
old_value,new_value,new_shipping_point,partner_name,text_type,text_type_action,table_data], function(err,results,output){
console.log("Hi");
if (err != null) {
console.log("error",err.message);
var user = req.ge('User');
var scriptName = path.basename(__filename);
var value = {err: err.message, user: user, scriptName:scriptName};
axios.post(configData.SERVER_URL + "/api/logToDB", value);
res.status(400).json(err.message);
} else {
console.log("Data successfully added in Main Table & Sub Table");
res.json("Data successfully added in Main Table & Sub Table");
}
});
});
});
module.exports = router;
This results in the following output:
Hi
Data successfully added in Main Table & Sub Table
POST /api/add/new 200 44.882 ms - 51
Hi
Data successfully added in Main Table & Sub Table
_http_outgoing.js:558
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they
are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:558:11)
at ServerResponse.header (C:\Users\C572103\Documents\One
Order Change\OOC Portal\backend\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\express\lib\response.js:267:15)
at C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\routes\uploadData.js:58:13
at Object.callback (C:\Users\C572103\Documents\One Order
Change\OOC Portal\backend\node_modules\msnodesqlv8\lib\procedure.js:233:13)
at onProcedureRaw (C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\msnodesqlv8\lib\procedure.js:60:22)
at C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\msnodesqlv8\lib\reader.js:199:15
at onStatementComplete (C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\msnodesqlv8\lib\driver.js:185:11)
at Object.end (C:\Users\C572103\Documents\One Order Change\OOC Portal\backend\node_modules\msnodesqlv8\lib\driver.js:318:19) {
code: 'ERR_HTTP_HEADERS_SENT'
}
The SQL operations are being performed using msnodesqlv8