I’m importing the excel sheet in DB with the assistance of Nodejs, I’m unable to authenticate and return the error as exist already the userid
when the merchandise.USER_ID
already exists in DB. my server goes crash and returns an error as Error [ERR_HTTP_HEADERS_SENT]: Can not set headers after they're despatched to the shopper
Please assist in the code how I repair this subject and make it, If the merchandise.USER_ID
already exists return error else insert.
var XLSX = require("xlsx");
const fs = require("fs");
attempt {
const transaction = await con.transaction();
var workbook = XLSX.readFile("myfile.xlsx");
let json_data = XLSX.utils.sheet_to_json(workbook.Sheets.Sheet1);
let rely = 0;
json_data.map(async (merchandise) =>
let stmt1 = await con.question("SELECT * FROM `desk` WHERE `user_id` = :userid", replacements: userid: merchandise.USER_ID , sort: con.QueryTypes.SELECT );
if (stmt1.size > 0)
await transaction.rollback();
return res.json( message: "exist already the userid" );
else
let stmt2 = await con.question("INSERT INTO `desk` (`user_id` , `user_name`) VALUES ( :user_id , :user_name)",
replacements:
user_id: merchandise.USER_ID,
user_name: merchandise.USER_NAME,
,
sort: con.QueryTypes.INSERT,
transaction: transaction,
);
rely++;
if (rely == json_data.size)
await transaction.commit();
return res.json( message: "file uploaded efficiently.." );
);
} catch (err)
await transaction.rollback();
return res.json( code: 500, message: msg: "SQL ERROR" , error: err.stack, standing: "error" );
A doable clue is from https://stackoverflow.com/questions/26307920/res-json-cant-set-headers-after-they-are-sent the place they are saying that res.json ends in sending a response to the shopper, so if you’re additionally trying to ship a response from another a part of the code then you’ll be trying to ship two units of responses, leading to that error message.