I've been struggling with this issue for quite some time now. I am developing an API using node.js through express, and all Restful functions are working fine for me except for the update function. When trying to update data, Postman gets stuck on "sending request" while the database is successfully updated. I suspect the problem lies in the response handling, as I have tried numerous combinations without success. Here's a snippet of my code:
Within the router
router.put('/:id', hlpJWT.authenticateToken, ctrProfile.updateInfo);
Within the controller
updateInfo = async function (req, res, next) {
const input = {
name: req.body.name,
password: await srvAuth.cryptPass(req.body.password),
..., // other input fields
};
let user = await srvProfile.updateProfile(input);
if (user) res.json(user);
else {
res.status(404);
res.json({ error: "user not found" });
}
};
Within the service
updateProfile = function (input) {
return new Promise((resolve, reject) => {
query(
"UPDATE users SET ? WHERE ?",
[
{
name: input.name,
... // more input fields
},
{ user_id: input.id },
],
function (error, result) {
if (error) {
return reject(error);
}
return resolve(result);
}
);
});
};
Thank you in advance to anyone who can offer assistance.
UPDATE: I eventually updated to express v5 and it resolved the issue, indicating that it was likely related to error handling. However, I was unable to pinpoint the exact cause.