so bassically, i am creating a router to update data in database, I started by checking the id in database using if function, at first it works when there is no such id I put in the path, but its come out with this error
BError [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\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:794:10) at ServerResponse.send (C:\Users\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:174:12) at ServerResponse.json (C:\Users\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:278:15) at C:\Users\jansen stanlie\Desktop\simple-app\controllers\todo.controller.js:79:21 at Array.forEach () at C:\Users\jansen stanlie\Desktop\simple-app\controllers\todo.controller.js:60:9 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async putToDo (C:\Users\jansen stanlie\Desktop\simple-app\controllers\todo.controller.js:54:2) { code: 'ERR_HTTP_HEADERS_SENT' } (node:12008) UnhandledPromiseRejectionWarning: 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\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:794:10) at ServerResponse.send (C:\Users\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:174:12) at ServerResponse.json (C:\Users\jansen stanlie\Desktop\simple-app\node_modules\express\lib\response.js:278:15) at C:\Users\jansen stanlie\Desktop\simple-app\controllers\todo.controller.js:86:20 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async putToDo (C:\Users\jansen stanlie\Desktop\simple-app\controllers\todo.controller.js:54:2) (Use
node --trace-warnings ...
to show where the warning was created) (node:12008) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:12008) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
and after that I cannot update any data even though there is in the database
here is my code
const putToDo = async (req, res) => {
const body = req.body;
let id = req.params.id;
console.log(id);
let data = [];
let updatedData = [];
await db
.query("select * from suppliers")
.then((results) => {
data = results.rows;
})
.then(() => {
data.forEach((datas) => {
if (datas.id == id) {
db.query(
`UPDATE suppliers
SET Name = '${body.name}'
WHERE id = ${id};`
)
.then(() => {
db.query("select * from suppliers").then((updated) => {
updatedData = updated;
console.log(updatedData);
});
})
.then(() => {
res.status(200).json({
message: "Data Successfully Updated",
});
});
}
res.status(500).json({
message: "No data in database",
});
});
})
.catch((e) => {
console.log(e);
res.status(500).json({
message: "INTERNAL SERVER ERROR",
});
});};
where did I do wrong here...?
CodePudding user response:
The error says you are trying to set headers after already sending a response
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at
That is because your res.status(500)
res.status(500).json({message: "No data in database"});
Runs after the res.status(200).json()
res.status(200).json({ message: "Data Successfully Updated"})
Try adding an else
to your condition so only 1 or the other will execute.
if (datas.id == id) {
db.query(
`UPDATE suppliers
SET Name = '${body.name}'
WHERE id = ${id};`
)
.then(() => {
db.query("select * from suppliers").then((updated) => {
updatedData = updated;
console.log(updatedData);
});
})
.then(() => {
res.status(200).json({
message: "Data Successfully Updated",
});
});
} else {
res.status(500).json({
message: "No data in database",
});
}
Additionally, you are using res.status().json()
multiple times within a loop, but should likely only be sending 1 response back that includes all the data instead of a response for every item in the looped data.
CodePudding user response:
Try this
res.status(200).json()