无法显示由带有节点express和ejs的html页面上的express验证器生成的错误

问题描述 投票:1回答:1

我正在使用nodejs,ejs,express和express-validator。我正在尝试在我的注册页面上使用快速验证器(signup,ejs)。 Express-validator在对象数组中生成错误。当我console.log结果时(出现var错误),我得到:

      formatter: [Function: formatter],
      errors:
       [ { value: 'kl',
           msg: 'Username must be between 4 and 15 characters.',
           param: 'username',
           location: 'body' },
         { value: 'kl',
           msg: 'Password must be between 8 and 100 characters.',
           param: 'password',
           location: 'body' },
         { value: 'klsdflksdf',
           msg: 'Passwords must match.',
           param: 'confirmPassword',
           location: 'body' } ] }

[当我尝试使用For Of来迭代视图页面(signup.ejs)上的数组时,我得到了err不可迭代的错误。当我使用For In(如下所示)时,结果是在视图页面上显示了两个项目。第一个是Fromatoer,第二个是错误。如何获取味精:valuse显示?用替换不会在视图页面上产生任何结果。

这是我的意见页面:

    <%- include('includes/navigation.ejs') %>
    <% if (typeof errors !== "undefined") { %>
        <p>there are errors on page</p>
    <% for(const error in errors) { %>
    <div class="alert alert-warning">
      <li>  <%= error %>  </li>
    </div>
    <% } %>
    <% } %>
    <div class="container">
        <form class="signup-form" action="/signup" method="POST">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username" name="username" placeholder="Enter username">

            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" id="password" name="password" placeholder="Password">
            </div>
            <div class="form-group">
                <label for="confirmPassword">Confirm Password</label>
                <input type="password" class="form-control" id="confirmPassword" name="confirmPassword"
                    placeholder="Confirm Password">
            </div>

            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>

这里是我的控制器:

exports.postSignup = [
    check('username', 'Username field must not be empty.').not().isEmpty(),
    check('username', 'Username must be between 4 and 15 characters.').isLength(4, 15),
    check('password', 'Password must be between 8 and 100 characters.').isLength(8, 100),
    check('confirmPassword', 'Passwords must match.').equals('password'),
    (req, res, next) => {

        const username = req.body.username;
        const password = req.body.password;
        const confirmPassword = req.body.confirmPassword;

        const errors = validationResult(req);


        if (errors) {
            console.log(errors);

            res.render('signup', {
                pageTitle: 'Registration Error',
                errors: errors
            });
        } else {
            User.create({
                username: req.body.username,
                password: req.body.password
            })
                .then(result => {
                    res.render('login', { pageTitle: "Signup Successfull" });
                })
                .catch(err => console.log(err));
        };
    }
];
mysql node.js express ejs express-validator
1个回答
0
投票
const errors = validationResult(req);

您需要使用isEmpty()方法检查是否存在任何错误;和array()方法将errors转换为可迭代数组。错误处理程序通常看起来像:

const errorHandler = (req, res, next) => {
    let errors = validationResult(req);
    if (!errors.isEmpty()) {
        errors = errors.array();
        // some other stuff
    } else next();
};

来源:express-validator docs

© www.soinside.com 2019 - 2024. All rights reserved.