close

[Fixed] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Today We are Going To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in nodejs. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Error?

  1. How to Fix Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Error?

    To Fix Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Error just Call res.redirect with a return statement. Just call res.redirect with a return statement, so the next function was also being called immediately afterwards. You can use the below code. It will help you to solve this error. auth.annonymousOnly = function(req, res, next) { if (req.user) return res.redirect('/'); next(); };

  2. Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

    To Fix Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Error just Use status(). You have to use the below code to solve your error. just use status() return res.status(200).json({ data: result });

Solution 1 : Call res.redirect with a return statement

Just call res.redirect with a return statement, so the next function was also being called immediately afterwards. You can use the below code. It will help you to solve this error.

auth.annonymousOnly = function(req, res, next) {
    if (req.user) return res.redirect('/');
    next();
};

Solution 2 : Use status()

You have to use the below code to solve your error. just use status()

return res.status(200).json({ data: result });

Solution 3 : Add the return key word

Just add the return key word like: return res.redirect("/great"); It will help you.

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read These Solutions

Leave a Comment