ExpressJs:无法从车把模板访问对象属性

问题描述 投票:0回答:1

我的路线:

router.use(function(req, res, next) {
  res.locals.currentUser = req.user;
  next();
});

/* GET home page. */
router.get('/', function(req, res, next) {
  console.log(res.locals.currentUser.username); ==>> this is getting printed in console.
    res.render('index');
});

我的索引手把

{{currentUser}} ===> this is getting displayed
{{currentUser.username}} ===> this is not

我的用户架构

const UserSchema =
    new Schema({
        fullName: String,
        username: { type: String, required: true },
        password: { type: String, required: true },
        isMember: { type: Boolean, default: false },
        isAdmin: { type: Boolean, default: false }
    });

我正在尝试访问'currentUser'对象的属性之一。尽管对象本身会显示在模板中,但其属性不会显示。

javascript node.js express handlebars.js
1个回答
0
投票

更改此

res.locals.currentUser = req.user;

res.locals.currentUser = req.user.lean(); // add lean()
© www.soinside.com 2019 - 2024. All rights reserved.