Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Create a comment
router.post("/comment", async (req, res) => {
const { postId, content, commentId } = req.body;
...
// Check if the comment ID already exists
const commentIdFind = await Comment.find({ commentId });
if (!commentIdFind.length) {
res
.status(400)
.json({ success: false, msg: "The specified comment ID already exists." });
}
await Comment.create({
postId,
content,
commentId,
});
res.status(200).json({ success: true, msg: "The comment has been registered." });
});
This error occurs when the response headers are being sent to the client multiple times. It seems that you may have missed using the “return” statement in your “if” statements.
By not using “return”, the execution continues to the next line, causing multiple responses to be sent and resulting in the mentioned error.
To resolve this issue, make sure to add the “return” statement after sending the response in each “if” statement.
Remember to include the “return” statement to ensure that the response is sent and the execution stops after each “if” statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Create a comment
router.post("/comment", async (req, res) => {
const { postId, content, commentId } = req.body;
...
// Check if the comment ID already exists
const commentIdFind = await Comment.find({ commentId });
if (!commentIdFind.length) {
return res
.status(400)
.json({ success: false, msg: "The specified comment ID already exists." });
}
await Comment.create({
postId,
content,
commentId,
});
res.status(200).json({ success: true, msg: "The comment has been registered." });
});