I'm working on a simple web app using node-pg
and express.js
with a login page and a dashboard that displays some data from a database.
The dashboard and all it's data loads fine initially using the following function after the user inputs their username and password:
const queryLogin = (request, response) => {
var username = request.body.username;
var password = request.body.password;
if (username && 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 > 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("Invalid Account Type");
}
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
}, function(error) {
throw error;
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
}
The issue is that I have a link
in my navbar
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: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
The route I use to try and redirect to the home page is: app.get('/home', con.launchDashboard)
And the code for the function is:
const launchDashboard = (request, response) => {
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();
}
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.