title: When I send a GET request : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
body: <p>when I tried to send a get request to the url of my function (on a firebase project ex: http://project_url.com/api/post/id), this error shows</p>
  <pre><code> (node:1061) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  &gt;      at ServerResponse.setHeader (_http_outgoing.js:518:11)
  &gt;      at ServerResponse.header (/Users/salvatorebramante/beta/functions/node_modules/express/lib/response.js:771:10)
  &gt;      at ServerResponse.send (/Users/salvatorebramante/beta/functions/node_modules/express/lib/response.js:170:12)
  &gt;      at ServerResponse.json (/Users/salvatorebramante/beta/functions/node_modules/express/lib/response.js:267:15)
  &gt;      at /Users/salvatorebramante/beta/functions/handlers/posts.js:70:25
  &gt;      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  &gt;  (node:1061) 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: 2)
  </code></pre>
  <p>The function returns a json with the infos about a document stored in firebase cloud storage</p>
  <pre><code>exports.getPost = (req,res) =&gt; {
      let postData;
      db.doc(`/posts/${req.params.postId}`).get()
      .then(doc =&gt;{
          if(!doc.exists){
              return res.status(404).json({error : 'Post not found'})
          }
          postData = doc.data();
          postData.postId = doc.id;
          return db.collection('comments').orderBy('createdAt', 'desc').where('postId','==',req.params.postId).get();
      })
      .then(data =&gt;{
          postId.comments = [];
          data.forEach(doc =&gt; {
              postData.comments.push(doc.data());
          })
          return res.json(postData);
      })
      .catch(err =&gt;{
          res.status(500).json({ error: err.code });             
          console.error(err);                                              
      });  
  
  };
  </code></pre>
  <p>While the error occurred, the get request response is 404, but I've checked that the document id is correct. How can I resolve it?</p>