18910140161

JavaScript-为什么当输入出错时错误不会显示-堆栈溢出

顺晟科技

2022-10-19 13:07:06

120

我使用节点js创建了一个注册页面,并作为一个示例进行了一些验证,如果密码与确认密码不匹配,它将返回到注册页面,并在(h1)标记下面列出错误,该标记是注册的,但它将返回到签名页面,没有任何错误 下面是我的代码:

module.exports.signup_post = (req,res)=>{
let {firstName,lastName,username,email,password,password2} = req.body;
console.log(
    firstName,
    lastName ,
    username,
    email,
    password,
    password2
    );

let errors =[];

if (!firstName || !lastName || !username || !email || !password || !password2){
    errors.push({message:'please enter the all fields'});

}
if (password.length<6){
    errors.push({message:'password should be at least 6 characters'})
}
if (password!= password2){
    errors.push({message:'passwords do not match'})
}
if (errors.length > 0){
    res.render('register');
    
}else{
    res.render('login')
  }

}


<ul>
        <% if (typeof errors != 'undefined'){ %>
            <% errors.forEach(error =>{ %>
                <li><% error.message %></li>
                <% }) %>
            <% } %>
    </ul>

顺晟科技:

将倒数第五行更改为:

module.exports.signup_post = (req,res)=>{
let {firstName,lastName,username,email,password,password2} = req.body;
console.log(
    firstName,
    lastName ,
    username,
    email,
    password,
    password2
    );

let errors =[];

if (!firstName || !lastName || !username || !email || !password || !password2){
    errors.push({message:'please enter the all fields'});

}
if (password.length<6){
    errors.push({message:'password should be at least 6 characters'})
}
if (password!= password2){
    errors.push({message:'passwords do not match'})
}
if (errors.length > 0){
    res.render('register');
    
}else{
    res.render('login')
  }

}


<ul>
        <% if (typeof errors != 'undefined'){ %>
            <% errors.forEach(error =>{ %>
                <li><% error.message %></li>
                <% }) %>
            <% } %>
    </ul>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航