无法访问 req.user 的属性 [英] can't access req.user's properties

查看:57
本文介绍了无法访问 req.user 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有登录系统的多人游戏.在我的 app.js 中,我有

I am creating a multiplayer game with login system. In my app.js I have

app.use(function (req, res, next) {
  res.locals.success_msg = req.flash('success_msg');
  res.locals.error_msg = req.flash('error_msg');
  res.locals.error = req.flash('error');
  res.locals.user = req.user || null;

  next();
}); 

使用它我可以访问我的用户.当我在那里写 console.log(req.user); 它工作正常.它显示我的用户的属性.例如:

Using which I can access my user. When I write there console.log(req.user); it works fine.It displays my user's properties. For example:

{ __v: 0,
  password: '$2a$10$fHVFeChN4xo67ioWMDfix.VOFLAbNu33xAoRb09b9QRwnmOJ389/O',
  username: 'a',
  email: 'example@gmail.com',
  name: 'a',
  _id: 5744c2b7835f90cc05d27ee5 }

但是,当我编写 console.log(req.user.username); 它显示错误:

However, when I write console.log(req.user.username); it displays error:

类型错误:无法读取未定义的属性用户名".

如果有任何帮助,我将不胜感激.非常感谢提前!

I'll be grateful for any help. Thanks a lot in advance!

推荐答案

我找到了我的解决方案.我只需要将我的 module.exports 放在我的 app.js 中.

I found my solution. I just needed to put my module.exports in my app.js.

这篇关于无法访问 req.user 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆