变量未定义(错误)

时间:2020-10-29 19:04:46

标签: javascript node.js express ejs


我尝试通过使用ejs模板引擎将错误变量(在本例中为修复程序)从服务器发送到客户端,在其中显示错误。

变量列出了medainte push的错误,我在error.ejs中检索到它们

问题是它无法识别

非常感谢您。


user.controller.js

const usersController = {};

usersController.renderSignUpForm = (req, res) => {
    res.render('users/signup');
};

usersController.signUp = (req, res) => {
    let errors = [];
    const { name, email, password, confirmPassword } = req.body;
    console.log(req.body);
    if (password != confirmPassword) {
        errors.push({ text: 'Passwords do not match' });
    }
    if (password.length < 8) {
        errors.push({ text: 'Passwords must be a least 8 characters.' });
    }
    if (errors.length > 0) {
        res.render("users/signup", {
            errors,
            name,
            email,
            password,
            confirmPassword
        });
    } else {
        res.send('sign up successfully');
    }
};

module.exports = usersController; 

error.ejs

   <div class="container mt-4">
        <% for( let i = 1; i < errors.length; i++) {%>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <%= errors[i].text %>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                 <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <% } %>
    </div>

user.router.js

const { Router } = require('express');
const router = Router();

//Importing Controllers
const { renderSignUpForm, signUp, renderSignInForm, signIn, logOut } = require('../controllers/users.controllers');

router.get('/users/signup', renderSignUpForm);

router.post('/users/signup', signUp);

router.get('/users/signin', renderSignInForm);

router.post('/users/signin', signIn);

router.get('/users/logout', logOut);

module.exports = router;

signup.ejs

<%- include('../partials/head.ejs')%>
    <div class="container mt-4">
        <div class="row">
            <div class="col-md-4 mx-auto">
                <div class="card">
                    <div class="card-header">
                        <i class="fa fa-user-plus mr-2" aria-hidden="true"></i> Account Register
                    </div>
                    <div class="card-body">
                        <form action="/users/signup" method="POST">
                            <div class="form-group">
                                <label for="inputName">Name<i class="fa fa-user ml-2" aria-hidden="true"></i></label>
                                <input type="text" name="name" class="form-control" id="inputName" placeholder="Name" autocomplete="off" autofocus>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail">Email address<i class="fa fa-envelope-o ml-2" aria-hidden="true"></i></label>
                                <input type="email" name="email" class="form-control" id="inputEmail" placeholder="Email" autocomplete="off">
                            </div>
                            <div class="form-group">
                                <label for="inputPassword">Password<i class="fa fa-unlock-alt ml-2" aria-hidden="true"></i></label>
                                <input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password" autocomplete="current-password">
                            </div>
                            <div class="form-group">
                                <label for="confirmPassword">Confirm Password<i class="fa fa-unlock-alt ml-2" aria-hidden="true"></i></label>
                                <input type="password" name="confirmPassword" class="form-control" id="confirmPassword" placeholder="Confirm Password" autocomplete="current-password">
                            </div>
                            <div class="form-group">
                                <button class="btn btn-danger btn-block">Sign Up</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <%- include('../partials/error.ejs')%>
        <%- include('../partials/footer.ejs')%>

1 个答案:

答案 0 :(得分:0)

您开始从位置0开始运行错误数组; 数组从位置0开始,将其更改为从零开始

 <div class="container mt-4">
    <% for( let i = 0; i < errors.length; i++) {%>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <%= errors[i].text %>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
             <span aria-hidden="true">&times;</span>
        </button>
        </div>
        <% } %>
</div>