1

所以,这以前有效,突然决定停止工作,我不知道为什么。

编辑:更新代码以显示我现在得到的

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

  // let query
  let query;

  // Start email checks
  req.check('email', 'Email is not valid.')
  .isEmail()
  .custom(async value => {
    query = {email: value};
    User.findOne(query).then(user => {
      if (user) return false;
    });
  }).withMessage('Email is in use.');

  // Start username checks
  req.check('username', 'Username is required.')
  .notEmpty()
  .isLength({ min: 5, max: 15}).withMessage('Username requires 5-15 alphanumberic characters.')
  .isAlphanumeric().withMessage('Username must be alphanumeric only.')
  .custom(async value => {
    query = {username: value}
    User.findOne(query).then(user => {
      if (user) return false;
    });
  }).withMessage('Username is in use.');

  // Start password checks
  req.check('password', 'Password is required.')
  .notEmpty()
  .isLength({min: 5}).withMessage('Password must be atleast 5 characters long.');
  req.check('confirmPassword', 'Confirm Password is required.')
  .notEmpty()
  .custom(value => value === req.body.password).withMessage('Password must match');


  const errors = await req.getValidationResult();
  //console.log(errors);
  if (!errors.isEmpty()) {
      res.render('index', {
      errors: errors.mapped()
    });
  } else {
    let newUser = new User({
      email: req.body.email,
      username: req.body.username,
      password: req.body.password,
    });
    let hash = bcrypt.hashSync(req.body.password, 10);     
    newUser.password = hash;
    newUser.save(err => {
      if (err) {
        console.log(err);
      } else {
        res.render('index', {
          success: 'Registration Successful'
        });
      }
    });
  }
});

所以它很清楚它与我的自定义检查有关,我不知道为什么。

编辑:似乎有混乱。检查工作正常,我遇到的问题是它在我想要的时候填充错误。如果我尝试使用相同的电子邮件注册,它将拉起用户并通过我的 if 语句。如果我使用 Promise.reject() 它不起作用。如果我使用false,它不起作用。同样,检查本身有效,错误处理似乎不是。

编辑二:所以我尝试了这种方法(所有其他代码仍然相同)

// Start email checks
req.checkBody('email', 'Email is not valid.')
.isEmail()
.custom(value => {
  query = {email: value}
  User.findOne(query).then(user => {
    if (user) console.log('Email Exists'); return false;
  });
}).withMessage('Email in use.');

// Start username checks
req.check('username', 'Username is required.')
.notEmpty()
.isLength({ min: 5, max: 15}).withMessage('Username requires 5-15 alphanumberic characters.')
.isAlphanumeric().withMessage('Username must be alphanumeric only.')
.custom(value => {
  query = {username: value}
  User.findOne(query).then(user => {
    if (user) console.log('Username Exists'); return false;
  });
}).withMessage('Username in use.');
4

2 回答 2

0

终于找到了一个答案,顺便也让它更容易阅读。我最终制作了自己的自定义验证器:

  customValidators: {
    emailExists: (email) => {
      let query = {email: email};
      return new Promise((resolve, reject) => {
        User.findOne(query, (err, results) => {
          if (results === null) {
            resolve(err);
          }
          reject(results);
        });
      });
    },
    userNameExists: (username) => {
      let query = {username: username};
      return new Promise((resolve, reject) => {
        User.findOne(query, (err, results) => {
          if (results === null) {
            resolve(err);
          }
          reject(results);
        });
      });
    }
  },

然后:

  req.check('email', 'This email is in use.').emailExists();
  req.check('username', 'Username is in use.').userNameExists();
  req.asyncValidationErrors().then(() => {
    console.log('No errors');
    let newUser = new User({
      email: req.body.email,
      username: req.body.username,
      password: req.body.password,
    });
    let hash = bcrypt.hashSync(req.body.password, 10);     
    newUser.password = hash;
    newUser.save(err => {
      if (err) {
        console.log(err);
      } else {
        res.render('index', {
          success: 'Registration Successful'
        });
      }
    });
  }).catch(errors => {
    res.render('index', {
      errors: errors
    });
  });
于 2018-12-14T15:27:53.553 回答
0

这应该有效。由于 node.js 是非渲染阻塞的,db 查询在进入下一步之前可能无法完成。您可以使用我在下面发布的格式,也可以尝试使用 async 库,在这种情况下,await关键字应该放在前面User.findOne

 User.findOne(query).then(user=>{
     if(user) return false
    }).catch(err => console.log(err))
于 2018-12-14T08:27:31.300 回答