title: ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client
body: <p>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.</p>
  
  <p>I've read other posts and tried them out as well, and none of them worked.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/43055600/app-get-is-there-any-difference-between-res-send-vs-return-res-send">app.get - is there any difference between res.send vs return res.send</a></li>
  <li><a href="https://stackoverflow.com/questions/49740710/error-err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-cli">Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client</a></li>
  <li><a href="https://stackoverflow.com/questions/47753743/cannot-set-headers-after-they-are-sent-to-the-client">Cannot set headers after they are sent to the client</a></li>
  </ul>
  
  <p>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.</p>
  
  <p>This is my stack trace:</p>
  
  <blockquote>
    <p>(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.</p>
    
    <p>Server Running on port 5000<br>
    MongoDB Connected Error<br>
    [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the
    client<br>
    &nbsp;&nbsp;at validateHeader (_http_outgoing.js:503:11)<br>
    &nbsp;&nbsp; at ServerResponse.setHeader (_http_outgoing.js:510:3)<br>
    &nbsp;&nbsp; at ServerResponse.header (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:767:10)<br>
    &nbsp;&nbsp; at ServerResponse.json (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:264:10)<br>
    &nbsp;&nbsp; at Profile.findOne.then.profile (/Users/lourdesroashan/code/github/devlog/routes/api/profile.js:27:30)<br>
    &nbsp;&nbsp; at &lt;anonymous&gt;</p>
  </blockquote>
  
  <p>This is my server code:</p>
  
  <pre><code>router.get("/userprofile", passport.authenticate('jwt', { session: false }), (req, res) =&gt; {
  
    Profile.findOne({ user: req.user.id }).then(profile =&gt; {
      if (!profile) {
        return res.status(404).json({ error: "No Profile Found" });
      }
      else {
        res.json(profile);
      }
    }).catch(err =&gt; {
      console.log(err);
    })
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>Thank you so much in advance! :)</p>
  
  <p>Full Code at: <a href="https://github.com/lourdesr/devlog" rel="noreferrer">https://github.com/lourdesr/devlog</a></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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!</p>