ChatGPT解决这个技术问题 Extra ChatGPT

ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

I'm facing this weird issue in NodeJS when using with Passport.js, Express and Mongoose. Basically, I get an error saying "Cannot set headers after they are sent to the client" even though I don't send more than one header.

I've read other posts and tried them out as well, and none of them worked.

app.get - is there any difference between res.send vs return res.send

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

Cannot set headers after they are sent to the client

I've dug through github issues and I can't seem to find a solution. I get the problem that this error is triggered when I send multiple response headers, but the fact is that I am not sending multiple headers. It seems just weird.

This is my stack trace:

(node:9236) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. Server Running on port 5000 MongoDB Connected Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at validateHeader (_http_outgoing.js:503:11) at ServerResponse.setHeader (_http_outgoing.js:510:3) at ServerResponse.header (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:767:10) at ServerResponse.json (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:264:10) at Profile.findOne.then.profile (/Users/lourdesroashan/code/github/devlog/routes/api/profile.js:27:30) at

This is my server code:

router.get("/userprofile", passport.authenticate('jwt', { session: false }), (req, res) => {

  Profile.findOne({ user: req.user.id }).then(profile => {
    if (!profile) {
      return res.status(404).json({ error: "No Profile Found" });
    }
    else {
      res.json(profile);
    }
  }).catch(err => {
    console.log(err);
  })
});

I understand what the error means, but from what I know, I don't think I am sending multiple headers, I even checked by console.log that only one of the blocks is run.

Thank you so much in advance! :)

Full Code at: https://github.com/lourdesr/devlog

EDIT:

I figured it out. It was a problem in my passport.js while trying to get the authenticated user. I forgot to use 'return' on the 'done' method, which had caused it. Just added the return statement and it worked!

It seems unlikely that the server code you show is causing the error about headers being sent. There must be some other code that is causing that error. That particular error occurs whenever you try to send more than one response to the same request and is usually caused by improper asynchronous code.
Since you found a separate solution from the jfriend00's suggestions, please post your own answer to your question and accept it.

j
jfriend00

That particular error occurs whenever you try to send more than one response to the same request and is usually caused by improper asynchronous code.

The code you show in your question does not appear like it would cause that error, but I do see code in a different route here that would cause that error.

Where you have this:

if (!user) {
  errors.email = "User not found";
  res.status(404).json({ errors });
}

You need to change it to:

if (!user) {
  errors.email = "User not found";
  res.status(404).json({ errors });
  // stop further execution in this callback
  return;
}

You don't want the code to continue after you've done res.status(404).json({ errors }); because it will then try to send another response.

In addition, everywhere you have this:

if (err) throw err;

inside an async callback, you need to replace that with something that actually sends an error response such as:

if (err) {
    console.log(err);
    res.sendStatus(500);
    return;
}

throwing inside an async callback just goes back into the node.js event system and isn't thrown to anywhere that you can actually catch it. Further, it doesn't send a response to the http request. In otherwords, it doesn't really do what the server is supposed to do. So, do yourself a favor and never write that code in your server. When you get an error, send an error response.

Since it looks like you may be new here, I wanted to compliment you on including a link to your full source code at https://github.com/lourdesr/devlog because it's only by looking at that that I was able to see this place where the error is occuring.


@lourdesr - Did this explain it to you and answer your question?
It did give a lot more clarify. However, after a few hours, I was able to figure out the problem. It was lurking around in my passport.js file, while trying to get the authenticated user - I did not add a return statement in one of the "done" methods, this had caused the multiple headers error. Thank you for helping out!
Great, you've clarified the problem.
This is a good example of the pitfalls of the pre-async/await way of doing things in Node
Thanks Bro! After 50 minutes i finally find a way!
C
Chunky Chunk

I was receiving this error because of a foolish mistake on my part. I need to be more careful when referencing my other working code. The truly embarrassing part is how long I spent trying to figure out the cause of the error. Ouf!

Bad:

return res
  .send(C.Status.OK)
  .json({ item });

Good:

return res
  .status(C.Status.OK)
  .json({ item });

I don't see a difference between your good and bad example. @TheDarkIn1978
The difference is the "bad" example I'm calling .send("ok") while on the "good" example i'm calling .status("ok"). Chaining a .json call after .send caused the error. It was a careless copy/paste mistake from some of my other code.
You saved meeee!
cant believe so many people making the same mistake and not being able to spot the difference
M
Mohammad Reza Mrg

Use ctrl + F hotkey and find all 'res.' keywords then replace them with a prefix of 'return ',

change  all 'res.' to  'return res.'

something like this:

res.send() change to --> return res.send()

maybe you have 'res.' in some block, like if() statement


I
Ivan of uganda

Sorry for the Late response, As per the mongoose documentation "Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. However, unlike promises, calling a query's .then() can execute the query multiple time"

so to use promises

mongoose.Promise = global.Promise //To use the native js promises

Then

var promise = Profile.findOne({ user: req.user.id }).exec()
promise.then(function (profile){
    if (!profile) {
      throw new Error("User profile not found") //reject promise with error
    }
    return res.status(200).json(profile) //return user profile      
}).catch(function (err){
    console.log(err); //User profile not found
    return res.status(404).json({ err.message }) //return your error msg
})

here is an nice article about switching out callbacks with promises in Mongoose

and this answer on mongooses promise rejection handling Mongoose right promise rejection handling


E
Eugène Beliaev

There is a simple fix for the node error [ERR_HTTP_HEADERS_SET]. You need to add a return statement in front of your responses to make sure your router exits correctly on error:

router.post("/", async (req, res) => {
    
    let user = await User.findOne({email: req.body.email});   
    if (!user) **return** res.status(400).send("Wrong user");
    
});

D
Dharman

Because of multiple response sending in your request. if you use return key word in your else condition your code will run properly

if (!profile) {
    return res.status(404).json({ error: "No Profile Found" });
}
else {
    **return** res.json(profile);
}

佚名

I got the same error using express and mongoose with HBS template engine. I went to Expressjs and read the docs for res.render, and it says // if a callback is specified, the rendered HTML string has to be sent explicitly. So I wasnt originally sending my html explicitly in the callback,. This is only for a contact form btw, not login info, albeit GET

//Original
let { username, email } = req.query;  //My get query data easier to read

res.status(200).render('index', { username, email });

//Solution without error. Second param sending data to views, Third param callback

res.status(200).render('index', { username, email }, (err, html)=>{
      res.send(html);
 });

s
sina

you have to enable Promises in your programm, in my case i enabled it in my mongoose schema by using mongoose.Promise = global.Promise . This enables using native js promises.

other alternatives to this soloution is :

var mongoose = require('mongoose');
// set Promise provider to bluebird
mongoose.Promise = require('bluebird');

and

// q
mongoose.Promise = require('q').Promise;

but you need to install these packages first.


C
Christhopher Lion

My problem besides not returning, i was forgetting to await an asynchronous function in the handler. So handler was returning and after a bit the async function did its thing. 🤦🏻‍♀️

Before:

req.session.set('x', {...});
req.session.save();
return req.status(200).end();

When i needed to await:

req.session.set('x', {...});
await req.session.save();
return req.status(200).end();

B
Bilal Amin

In react, if your are calling the function in useEffect hook, make sure to add a dependency to the dependency Array.


E
ErikaPeterson007

I had this error from an if statement not having an else block.

if(someCondition) {
await () => { } 

}

await () => { } 

I changed the above to this below and solved my issue

if(someCondition) {
await () => { } 

} else { 
await () => { } 
}



R
Rajesh Khadka

This also happen when you tries to send the multiple response for a same request !! So make sure you always use return keyword to send response to client inorder to stop the further processing !!


Y
Yash Jadhav

Where you have this:

if (!user) { errors.email = "User not found"; res.status(404).json({ errors }); }

You need to change it to:

if (!user) { errors.email = "User not found"; return res.status(404).json({ errors }); }


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now