Saturday, 18 March 2017

error handling nodejs e1100

i'm trying to display message if email exists but the way i did it the message appear only after refreshing page.what i'm doing wrong?

router.post("/register",(req,res)=>{

const name = req.body.name,
    email = req.body.email,
    username = req.body.username,
    password = req.body.password,
    password2 = req.body.password2


req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);


    const newUser = new User({
        name: name,
        email:email,
        username: username,
        password: password
    });

    User.createUser(newUser, function(err, user){

if (err) { let errors = req.validationErrors();

  if (err.code === 11000) {
     req.flash('error_msg', 'Email already exists ');
  }

res.render('register', { errors:errors });

} else {
    req.flash('success_msg', 'You are registered and can now login');

    res.redirect('/users/login');
}

});

});



via thomas g

No comments:

Post a Comment