title: Trouble solving: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
body: <p>I'm working on a simple web app using <code>node-pg</code> and <code>express.js</code> with a login page and a dashboard that displays some data from a database.</p>
  <p>The dashboard and all it's data loads fine initially using the following function after the user inputs their username and password:</p>
  <pre><code>const queryLogin = (request, response) =&gt; {
      var username = request.body.username;
      var password = request.body.password;
      
      if (username &amp;&amp; password) {
  
          Promise.all([
              pool.query('SELECT * FROM jobdesc WHERE complete=0 AND customer=$1 ORDER BY date_rec ASC LIMIT 10', ['somecustomer']),
              pool.query('SELECT * FROM accounts WHERE email=$1 AND password=$2', [username, password],)
            ]).then(function([orderResults, results]) {
              if (results.rows.length &gt; 0) {
                  request.session.loggedin = true;
                  request.session.username = username;
                  var user = username;
                  var orderObj = orderResults.rows;
                  if (results.rows[0].account_type == 1) {
                      request.session.account_type = 1;
                      response.render('pages/index');
                  } else if (results.rows[0].account_type == 0) {
                      request.session.account_type = 0;
                      response.render('pages/dashboard', {
                          user: user,
                          orderObj: orderObj
                      });
                  } else {
                      console.log(&quot;Invalid Account Type&quot;);
                  }
              } else {
                  response.send('Incorrect Username and/or Password!');
              }           
              response.end();
            }, function(error) {
              throw error;
            });
          
      } else {
          response.send('Please enter Username and Password!');
          response.end();
      }  
  }
  </code></pre>
  <p>The issue is that I have a <code>link</code> in my <code>navbar</code> that redirects to the dashboard (in the case that the user navigates to a different page and wants to return to the home page). When pressed I get the following error: <code>Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client</code></p>
  <p>The route I use to try and redirect to the home page is: <code> app.get('/home', con.launchDashboard)</code></p>
  <p>And the code for the function is:</p>
  <pre><code>const launchDashboard = (request, response) =&gt; {
  
      if (request.session.loggedin) {
          Promise.all([
              pool.query('SELECT * FROM jobdesc WHERE complete=0 AND customer=$1 ORDER BY date_rec ASC LIMIT 10', ['somecustomer'])
          ]).then(function([orderResults]) {
              if (request.session.account_type == 1) {
                  return response.render('pages/index');
              } 
              else if (request.session.account_type == 0) {
                  var user = request.session.username;
                  var orderObj = orderResults.rows;
                  
                  return response.render('pages/dashboard', {
                      orderObj: orderObj,
                      user: user
                  });
              }
          })
      } else {
          response.render('pages/login');
      }
      response.end();
  }
  </code></pre>
  <p>I'm unsure of why I am getting this error, I am confused with where I am setting headers after they are sent to the client.</p>