【问题标题】:ejs not parsing User objectejs不解析用户对象
【发布时间】:2019-09-28 19:46:58
【问题描述】:

我的 app.js 看起来类似于

initialize passport etc
///////////////////////////////////////////
//middleware to pass authuser to req locals
app.use(function (req, res, next) {
  res.locals.authUser = req.user;
  next();
});

路由器.js

router.get('/',function(req,res){
    console.log(res.locals.authUser);
    res.render('index');
});

index.ejs 视图 <%= authUser.first_name %>

在 ejs 中,first_name 没有被解析,但如果我放置这个 <%= authUser %> 它会呈现

{ 
  is_admin: true,
  _id: 5d8cb3702e35aa2f5cbaabf8,
  first_name: 'ruban',
  last_name: 'shah',
  username: 'admin',
  email: 'admin@example.com',
  password: 'xxxxxx' 
}

这意味着 authUser 是 Object 但它不打印 authUser.first_name is ejs

但奇怪的是它呈现 authUser.username

【问题讨论】:

  • 任何属性是否有效?

标签: node.js express ejs


【解决方案1】:

我最好的猜测是,当您在用户登录后将用户添加到 sesison 时,您不是在添加用户对象,而是在添加 JSON 字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多