ExpressJs: невозможно получить доступ к свойству объекта из шаблона руля - PullRequest
0 голосов
/ 26 апреля 2020

Мои маршруты:

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');
});

Мой index.handelbars

{{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' собственность объекта. Хотя сам объект отображается в шаблоне, а его свойство - нет.

1 Ответ

0 голосов
/ 30 апреля 2020

Измените

res.locals.currentUser = req.user;

на

res.locals.currentUser = req.user.lean(); // add lean()
...