2

below is register route

 router.post("/register", function(req, res){
var newUser=    new User({username:req.body.username});
   User.register(newUser,req.body.password, function(error,user){
       if(error){
          req.flash("error", error.message);
            return res.render("register.ejs");
       }else{
           passport.authenticate("local")(req, res ,function(){
                req.flash("success","Welcome to PhotoDiary, " + user.username);
               res.redirect("/diary");
           });
       }
   });
});

below is passport-local

app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();

i put flash-connect in register route req.flash("error", error.message); when i sign up it shows the message after another refresh,not on the same page

Harsh kumar
  • 55
  • 10

1 Answers1

0

Its not a good practice to use res.locals for sending errors to views, Assuming You are using EJS or Pug you can also send your messages and other data to view file in this way.

router.post("/register", function(req, res){
var newUser=    new User({username:req.body.username});
User.register(newUser,req.body.password, function(error,user){
       if(error){
           req.flash("error", "An Error Has Occured");
           var data = {
              page_title : 'Register', // Set Page Title Dynamically
              message : req.flash('error'), // Set Message
           };
            return res.render("register.ejs" , data);
       }else{
           passport.authenticate("local")(req, res ,function(){
           req.flash("success","Welcome to PhotoDiary");
           var data = {
              page_title : 'Register', // Set Page Title Dynamically
              username : user.username,
              message : req.flash('success'), // Set Message
           };
              res.redirect("/diary", data);
           });
       }
   });
});

You will have access to the data in your view file using the variables in this way (Example for EJS)

<Head>
   <title><%= page_title %></title>
</head>

<div class="alert alert-success">
      <%= message %> <%= username %> 
</div>

<h1> Welcome </h1>
Gaurav Kumar
  • 698
  • 1
  • 8
  • 15
  • 1
    thanks for giving the other method @gaurav but my question is why connect-flash message in my method show the error message after one more refresh not on the same page when i click on register form..whats wrong with connect-flash in my code?? – Harsh kumar Feb 05 '18 at 10:57
  • @Harh .... I have strongly believe that Its not an issue with Connect-flash but the methodology used by you, as far as i know res.locals middleware value is available on response of the next request that is why its showing you in next request. either Use the way as mentioned by me you will get that flash message in same request or else if you wanna go by your way pass res.locals & message instead of req.locals just before redirecting in the route. Hope you understand what i said – Gaurav Kumar Feb 05 '18 at 12:18
  • yeah it work. .i replace return res.render("register.ejs"); to res.redirect("/register"); and your method is also work. .thanks for your help @gaurav :) – Harsh kumar Feb 06 '18 at 19:02
  • Happy to Help, Jai Hind – Gaurav Kumar Feb 06 '18 at 23:16
  • One more thing my method also sets locals in an hidden way so if ever the variable shows undefined in ejs file use variable as locals.variable to access its data in there. I too don't why it happens sometimes it works directly sometimes after adding locals prefix. – Gaurav Kumar Feb 06 '18 at 23:20
  • yeah i understand your method completely. .i put message variable to locals it work :) jai hind – Harsh kumar Feb 07 '18 at 07:13