Home > Net >  Is there a way to return multiple errors from a pug js form using node js and express
Is there a way to return multiple errors from a pug js form using node js and express

Time:08-10

I am trying to validate inputs from the a form on a node js web server using pug js and express js. 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();
    }
});

CodePudding user response:

You cannot bind multiple else statements to a single if statement.

What you can do instead is to use an if statement for every error condition and take necessary actions. 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.

  • Related