首页 > 解决方案 > Nodejs错误消息不断弹出注册系统

问题描述

所以我是 NodeJS 和 JS 的新手,我正在尝试创建一个注册系统。我正在尝试处理我的错误消息。现在,每次在正确填写所有字段后单击注册时,我都会得到以下输出,如图所示。但是,我不确定为什么要在出生日期字段中输入全名以及 2000-11-11,所以我不应该根据我的代码收到这些错误消息。

  router.post('/register', function(req, res, next) {
  var name = req.body.name; 
  var email = req.body.email;
  var password = req.body.password; 
  var password2 = req.body.password2; 
  var bday = req.body.bday; 
  var birthday = moment(bday); 
  let errors = []; 

  // Form Validator
  if (!req.body.name) {
    errors.push({ msg: 'Please fill in all required fields' }); 
  }

  if (req.body.email && !req.body.email.isEmail()) {
    errors.push({ msg: 'Please provide an appropriate email' }); 
  }  

  if (req.body.password2 && req.body.password && req.body.password2 != req.body.password) {
    errors.push({ msg: 'Please provide passwords and make sure they match' }); 
  }

  if (req.body.bday && !birthday.isValid()) {
    errors.push({ msg: 'Date of Birth must be in appropriate format' }); 
  }

  if (moment().diff(birthday, 'years') < 13) {
    errors.push({ msg: 'User must be at least 13 years of age' }); 
  }

  if(errors.length > 0){
    res.render('register', {
      errors, 
      name, 
      email, 
      password, 
      password2, 
      bday
    });
  } else{
    var newUser = new User({
      name: name,
      email: email,
      password: password,
      bday: bday, 
    });

错误信息:

在此处输入图像描述

标签: javascriptnode.jsexpresserror-handling

解决方案


推荐阅读