title: Error [ERR_HTTP_HEADERS_SENT] after change in database
body: <p>I want to check in Firestore database if user paid or not. so everytime i go to the <code>/paid</code> route i check in databse if he paid or not.
  but if he requests the route and let's say he didn't pay. and the i change the database so that he did pay then i get the error</p>
  
  <p>i tried checking if i had sent headers twice but i didn't find anything.</p>
  
  <p>the app works again once i restart the server.</p>
  
  <p>this is the app.js code:</p>
  
  <pre><code>app.get("/paid", verifyUser, (req, res) =&gt; {
    const claims = res.locals.decodedClaims;
    const uid = claims.uid;
    const email = claims.email;
    const userRef = db.collection("users").doc(uid);
  
    userRef.get().then(docSnapshot =&gt; {
      if (docSnapshot.exists) {
        userRef.onSnapshot(doc =&gt; {
          // do stuff with the data
          if (doc.data().paid) {
            res.send("paid");
            return;
          }
  
          res.send("Didn't pay");
        });
      } else {
        userRef.set({ paid: false, name: email }); // create the document
  
        res.send("&lt;h1&gt;hello world&lt;/h1&gt;&lt;br&gt;" + uid);
      }
    });
  });
  </code></pre>
  
  <p>verifyUser middleWare (just for clarification):</p>
  
  <pre><code>//import firebase admin module
  const admin = require("firebase-admin");
  
  module.exports = {
    // check if cookie is available and if cookie is valid
    verifyUser: (req, res, next) =&gt; {
      var sessionCookie = req.cookies.session || "";
  
      if (sessionCookie) {
        admin
          .auth()
          .verifySessionCookie(sessionCookie, true)
          .then(function(decodedClaims) {
            //decodedClaims is not nessecary now
            res.locals.decodedClaims = decodedClaims;
            return next();
          })
          .catch(function(err) {
            // Redirect to login page on error.
            console.log(err);
  
            res.redirect("/loggedOutFail");
          });
      } else {
        // Redirect to login page when no session cookie available.
        res.redirect("/loggedOutNoCookie");
      }
    }
  };
  
  </code></pre>
  
  <p>this is the error:</p>
  
  <pre><code>_http_outgoing.js:470
      throw new ERR_HTTP_HEADERS_SENT('set');
      ^
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:470:11)
      at ServerResponse.header (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:771:10) 
      at ServerResponse.contentType (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:599:15)
      at ServerResponse.send (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:145:14)   
      at userRef.onSnapshot.doc (C:\Users\mendi\Desktop\firebase-auth\app.js:306:13)
      at DocumentWatch.watch.onSnapshot [as onNext] (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\reference.js:404:21)
      at DocumentWatch.pushSnapshot (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:519:18)
      at DocumentWatch.onData (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:403:26)
      at BunWrapper.currentStream.on (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:372:26)
      at BunWrapper.emit (events.js:189:13)
  </code></pre>
  
  <p>result is supposed to be that at every request i can change the data in the database and everything still works fine.</p>