我正在尝试为我尝试构建的登录页面实现一个非常基本的错误消息。post请求的代码如下。
app.post('/login',function(req,res){
User.findOne({username:req.body.user.username},function(err,info){
if(err){
req.flash('error','There was an error on our end. Sorry about that.');
res.redirect('/');
}
if(info==null){
req.flash('error', 'Incorrect Username. Please try again, or make an 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');
}
}
});
});
尽管确实发生了重定向,但在触发 null 案例时,我从未看到错误消息。我的路线获取方法如下:
app.get('/login',function(req,res){
res.render('login',{
title: 'Login'
});
});