【发布时间】:2019-06-10 23:28:06
【问题描述】:
我试图仅在定义变量用户时,登录后显示 html,但无论我做什么,包含都不起作用
登录后,用户被重定向到'/'并填充了对象用户
Console.log 在第二次调用 '/' 路由时打印用户
我尝试了不同的语法,除了下面的一个:
if (user)...
if (typeof user !== "undefined")...
index.ejs:
<% var user; %>
<%- include('partials/head'); %>
<body>
<%- include('partials/navbar'); %>
<% if (user.length > 0) { %>
<%- include('home'); %>
<% }; %>
<%- include('partials/scripts'); %>
</body>
<%- include('partials/footer'); %>
路线:
router.get('/', function (req, res, next) {
let user = req.cookies['user'];
if(user){
console.log(user);
}
res.render('index', user);
});
module.exports = router;
登录方法的一部分(有效,正在将填充的对象发送到路由):
if (result == true) {
let token = jwt.sign({ user: newUser }, 'secretkey', { expiresIn: '60s' });
currentUser.jwt = token;
currentUser.logado = true;
res.cookie('user', currentUser);
res.redirect('/');
【问题讨论】: