Constantly Confused
Constantly Confused

Reputation: 635

Display error message to user (node.js)

So I've been trying to research online for solutions, and none of them seem to work either because I'm doing it wrong, or they don't work in my situation.

I have a webpage which gives the user a place to enter email/pass etc. When they press the submit, it calls a post function, which has all the validation contained within it. Like so:

app.post('/check', function(req, res){
    function emailCheck(email){
        //when theres an error: console.log(error), and return false
    }

    function passCheck(password){
        //when theres an error: console.log(error), and return false
    }

    if (passCheck == true && emailCheck == true){
        //enter user into database
    }
}

When there is an error, I want it to be displayed to the user, either by use of popup box, or just text positioned under the sign up box.

Any suggestions would be great.

Upvotes: 5

Views: 12503

Answers (2)

Amritpal Singh
Amritpal Singh

Reputation: 21

   //register User on post request from register form
router.post("/register", function(req, res){
    var newUser = new User({username: req.body.username});
    User.register(newUser, req.body.password, function(err, user){
        if(err){
           // console.log(err.message);
            req.flash("error", err.message);
            return res.render("register");
        }
        passport.authenticate("local")(req, res, function(){
             req.flash("success","You created a new User Account " +user.username);
           res.redirect("/campgrounds"); 
        });
    });
});

you can Install: npm install flash-connect

and use error.message in console log to display the errors

Upvotes: 2

Cynthia Hart
Cynthia Hart

Reputation: 129

I would make an ajax request from the client to the server. If the validation fails you can send data specifying the problem to the client and do what you'd like with it.

Upvotes: 2

Related Questions