Recently, I started working on NodeJs and managed to create multiple APIs for my application. Everything was running smoothly until I encountered a strange issue - a new API that I added in the same file as the others is being called twice when accessed from the front end. The first call is a preflight with an options method, followed by another pending request without a specified method. I have attached screenshots for reference: API Call
Below is the code snippet for the problematic API:
router.put("/carrousel-item/update/:id", fileUpload, (req, res) => {
req.getConnection((err, conn) => {
try {
const image = fs.readFileSync(
path.join(__dirname, "../images/" + req.file.filename)
);
const title_en = req.body.title_en;
const title_es = req.body.title_es;
const sub_title_color_1_en = req.body.sub_title_color_1_en;
const sub_title_color_1_es = req.body.sub_title_color_1_es;
const sub_title_color_2_en = req.body.sub_title_color_2_en;
const sub_title_color_2_es = req.body.sub_title_color_2_es;
try {
conn.query(
`UPDATE home_team_section SET ? WHERE id = ?`,
[
{
title_en: title_en,
title_es: title_es,
sub_title_color_1_en: sub_title_color_1_en,
sub_title_color_1_es: sub_title_color_1_es,
sub_title_color_2_en: sub_title_color_2_en,
sub_title_color_2_es: sub_title_color_2_es,
image: image,
},
],
(err, rows) => {
res.send(rows);
}
);
} catch (error) {
console.log(error);
}
} catch (error) {}
});
});
This issue has become a roadblock in my project progression as it prevents me from executing any new APIs successfully.
Note: I have already included the cors() middleware in my setup.
I would greatly appreciate any assistance you can provide in resolving this matter.