error.push({“”})不适用于把手形式

时间:2019-06-02 15:25:23

标签: javascript express mongoose handlebars.js express-handlebars

当提交我的HTML /车把表格时,它没有显示任何错误,我认为error.push()不起作用,并且页面什么也不做,只是停留在无限充电中,这是错误验证和我的路线:

const express = require('express')
const router = express.Router()
const mongoose = require('mongoose')
require('../models/User')
const User = mongoose.model('users')

router.get('/register' , (req, res) => {
    res.render('users/register')     
})

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

    let error = [];

    if(!req.body.name || typeof req.body.name == undefined || req.body.name == null){
        error.push({text: 'Invalid name!'})
    }

    if(!req.body.email || typeof req.body.email == undefined || req.body.email == null){
        error.push({text: 'Invalid e-mail'})
    }

    if(!req.body.passwd || typeof req.body.passwd == undefined || req.body.passwd == null){
        erros.push({text: 'Invalid password'})
    }

    if(req.body.passwd.lenght < 4){
        error.push({text: 'Password is too short'})
    }

    if(req.body.passwd != req.body.passwd2){
        error.push({text: 'Different passwords'})
    }

    if(error.lenght > 0){

        res.render('users/register', {error: error})

    }else{

    }

})


module.exports = router

这是我制作的HTML /把手形式:

{{#each error}}
    <div class="alert alert-danger">{{text}}</div>
{{else}}
{{/each}}
<h1>Registre sua conta:</h1>
<div class="card">
    <div class="card-body">
        <form action="" method="POST">
            <label for="name">Name: </label>
                <input type="text" name="name" class="form-control" required>
            <label for="email">E-mail: </label>
                <input type="email" name="email" class="form-control" required>
            <label for="passwd">Password: </label>
                <input type="password" name="passwd" class="form-control" required>
            <label for="passwd2">Repeat Password: </label>
                <input type="password" name="spasswd2" class="form-control" required>
            <button type="submit" class="btn btn-success mt-4">Create account</button>
        </form>
    </div>
</div>

0 个答案:

没有答案