我在 expressjs 应用程序中使用 express-validator 5.2.0。我在表单数据上实现了验证。但它不会捕获错误并给出空的错误对象。当验证运行时,它在我提交空表单时显示“无错误”。它应该遵循错误路径并且应该显示错误。
var express = require('express');
var router = express.Router();
const { check, validationResult } = require('express-validator/check');
router.post('/register', [
check('firstName', 'First Name is required').isEmpty(),
check('lastName', 'Last Name is required').isEmpty(),
check('username', 'User Name is required').isEmpty(),
check('password', 'Password is required').isEmpty()
], (req, res, next)=>{
let errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors.mapped());
console.log("errors")
return res.render('auth/register', { errors: errors.mapped() })
}else{
console.log('no errors')
return res.render('auth/login');
}