gpt4 book ai didi

node.js - 尝试使用nodejs expressjs mongodb找出post和get方法的解决方案

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:19 25 4
gpt4 key购买 nike

我尝试使用passportjs成功登录后将用户数据传递到仪表板页面。

post方法工作正常,但将用户数据传递到index.handlebars不成功。

有人可以检查我的代码并提供可能的解决方案吗?

//This is my POST method

app.post("/login", passport.authenticate('local', {
succesRedirect: "/",
failureRedirect: "/user/login"
}),
function (req, res) {
res.render('/', username: req.body.username);
});

//This is my GET method after successful login

app.get('/', ensureAuthenticated, function (req, res) {
res.render('index');
});


**index.handlebars** **displaying user data**
<html>
<body>
<div>
<h4>Welcome {{username}} to this page</h4>
</div>
</body>
</html>

最佳答案

Passport 将用户信息放入 req.user 中。所以你应该这样改变:

//This is my GET method after successful login 

app.get('/', ensureAuthenticated, function (req, res) {
res.render('index', { username: req.user.username });
});

关于node.js - 尝试使用nodejs expressjs mongodb找出post和get方法的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49891338/

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