gpt4 book ai didi

express - node.js connect-flash 消息不显示

转载 作者:行者123 更新时间:2023-12-05 01:08:53 24 4
gpt4 key购买 nike

我正在尝试为我尝试构建的登录页面实现一个非常基本的错误消息。 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'
});
});

最佳答案

您需要将 flash 消息传递到您的模板,并在那里自己呈现它们:

app.get('/login',function(req,res){
res.render('login',{
title : 'Login',
errors : req.flash('error')
});
});

如果您使用 EJS 模板(例如),您检查是否 errors包含一个值,并显示它:
<% if (errors) { %>
<p class="error"><%= errors %></p>
<% } %>

关于express - node.js connect-flash 消息不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com