Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo User Registration Adding Data to the Database

Help please

So when I fill out the form and submit, it does not redirect to profile, and throws an error "Validation has failed"

router.post('/register', function(req, res, next){
  if(
  req.body.email &&
  req.body.name &&
  req.body.favoriteBook &&
  req.body.password &&
  req.body.confirmPassword){


    // confirm that user typed same password twice
    if(req.body.password !== req.body.confirmPassword){
      var err = new Error('Passwords do not match');
      err.status = 400;
      return next(err);
    }


    var userData = {
        email: req.body.email,
        name: req.body.name,
        favoriteBook: req.body.favoriteBook,
        password: req.body.password
      };

      // use schema's `create` method to insert document into Mongo
      User.create(userData, function (error, user) {
        if (error) {
          return next(error);
        } else {
          return res.redirect('/profile');
        }
      });

    } else {
      var err = new Error('All fields required.');
      err.status = 400;
      return next(err);
  }


});

It looks like you need a space between "if" and "(req.body.password != req.body.confirmPassword) {." You also need to remove the ";" at the end of your code and it should work. I hope this helps.