express - node.js connect-flash message does not show up -
i trying implement pretty basic error message login page trying build. code post request follows.
app.post('/login',function(req,res){ user.findone({username:req.body.user.username},function(err,info){ if(err){ req.flash('error','there error on our end. sorry that.'); res.redirect('/'); } if(info==null){ req.flash('error', 'incorrect username. please try again, or make account'); res.redirect('/login'); } else{ if(req.body.user.password==info.password){ res.redirect('/users/' + info.username); } else{ req.flash('error', 'incorrect password'); res.redirect('/login'); } } }); });
i never see error message when trigger null case, although redirect occur. method route follows:
app.get('/login',function(req,res){ res.render('login',{ title: 'login' }); });
you need pass flash messages template, , render them there:
app.get('/login',function(req,res){ res.render('login',{ title : 'login', errors : req.flash('error') }); });
if you're using ejs templates (for example), check if errors
contains value, , show it:
<% if (errors) { %> <p class="error"><%= errors %></p> <% } %>
Comments
Post a Comment