I’ve created a signup form using mongoose and express 3
Its possible the user already exists with that username, in which case I get an err.code 11000 (duplicate key). How should I handle existing users?
This is what I’m doing now….but I’m not sure checking error code is best way:
user.save(function(err){
if ( err ) {
console.log(err);
console.log(err.code);
//duplicate key
if ( err.code == 11000 ) {
req.flash('error', 'User already exists');
res.redirect('/signup');
return;
}
}
res.locals.user = user;
req.session.user = user;
//res.locals.session = req.session;
res.redirect('/');
});
Is there a better way of doing this?
I have not tried this yet, but this is what I’m thinking will avoid causing an error: