简体   繁体   English

有没有办法使用节点 js 从 pug js 表单返回多个错误并表达

[英]Is there a way to return multiple errors from a pug js form using node js and express

I am trying to validate inputs from the a form on a node js web server using pug js and express js.我正在尝试使用 pug js 和 express js 验证节点 js web 服务器上表单的输入。 when there is only one problem with the users input it will return that error but if there is more then one issue with their input it only shows to first problem with their input.当用户输入只有一个问题时,它将返回该错误,但如果他们的输入存在多个问题,则仅显示其输入的第一个问题。 what would be the best way to return both errors to the user if for example their input did not pass the length validation and the regex?如果例如他们的输入没有通过长度验证和正则表达式,那么将这两个错误返回给用户的最佳方法是什么?

    if(inputPassHash == password_sha512Finished){
                console.log("true:  " + password_sha512Finished);
                if(req.body.captcha_input == req.session.captcha_value){
                    req.session.logged = true;
                    res.redirect("/");
                    res.end();
                } else {
                    console.log("captcha not valid");
                    res.redirect("/login?error=" + "captcha-not-valid");
                    res.end();
                }

            } else{
                console.log("false:  " + password_sha512Finished);
                res.redirect("/login?error=" + "incorrect-password");
                res.end();
            }
        } else {
            console.log("userid not valid!")
            res.redirect("/login?error=" + "userid-not-valid")
            res.end();
        }
    } else {
        console.log("username does not exist!");
        res.redirect("/login?error=" + "incorrect-username");
        res.end();
    }
});

You cannot bind multiple else statements to a single if statement.您不能将多个else语句绑定到单个if语句。

What you can do instead is to use an if statement for every error condition and take necessary actions.相反,您可以做的是对每个错误条件使用 if 语句并采取必要的措施。 It will check for every possible error(if present) and logs those error messages.它将检查每个可能的错误(如果存在)并记录这些错误消息。

And also, once you redirect and do the res.end() the further conditions will not be checked.而且,一旦您重定向并执行res.end() ,将不会检查进一步的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM