最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

ejs无法解析用户对象

IT培训 admin 6浏览 0评论

ejs无法解析用户对象

[我正在尝试学习快速编程,我来自PHP背景,我试图实现的目标是将经过身份验证的用户设置为res.locals变量,以便可以在eJS中轻松使用经过身份验证的用户变量。

MY app.js看起来类似于

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

router.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: '[email protected]',
  password: 'xxxxxx' 
}

这意味着authUser是对象,但不打印authUser.first_name是ejs

但很奇怪的是,它呈现authUser.username

回答如下:

[我的最佳猜测是,当您在用户登录后将用户添加到sesison时,不是在添加用户对象而是在添加JSON String。

ejs无法解析用户对象

[我正在尝试学习快速编程,我来自PHP背景,我试图实现的目标是将经过身份验证的用户设置为res.locals变量,以便可以在eJS中轻松使用经过身份验证的用户变量。

MY app.js看起来类似于

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

router.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: '[email protected]',
  password: 'xxxxxx' 
}

这意味着authUser是对象,但不打印authUser.first_name是ejs

但很奇怪的是,它呈现authUser.username

回答如下:

[我的最佳猜测是,当您在用户登录后将用户添加到sesison时,不是在添加用户对象而是在添加JSON String。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论