Here's my User database schema:
create table users (
id serial primary key,
name text NOT NULL,
username text UNIQUE NOT NULL,
password text NOT NULL,
admin_access boolean default 'FALSE'
);
Here's where I add a new user:
static add(name, username, password) {
return db.one(
`insert into users
(name, username, password)
values
($1, $2, $3)
returning id` , [name, username, password]
)
.then(data => {
return new User (data.id, name, username, password)
})
}
Here's where the form data is taken and I call User.add:
app.post('/addUser', protectRoute, (req, res) => {
const newName = req.body.name;
const newUsername = req.body.username;
const newPassword = req.body.password;
User.add(newName, newUsername, newPassword)
.catch(() => {
res.redirect(`/addUser`);
})
.then(newUser => {
req.session.user = newUser;
res.redirect(`users`);
})
})
The error I get when I add a new user is: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I'm not quite sure how to handle the error returned and give a message or alert of some kind that tells the admin that the username already exists