我使用Passport来验证我的项目。成功验证后,用户将被定向到" / home"。此时,请求具有用户对象。但是,一旦我重定向,req.user.email是未定义的。 :'(
exports.test= function (req, res) {
console.log(req.user);
console.log(req.user.id);
console.log(req.user.email);
}
终端显示:
ModelBase {
attributes:
{ id: 18,
first_name: 'test',
last_name: 'test',
email: 'test1@gmail.com',
password: '18fa9883b74578494b578048273e66d2edbf57b6',
active: 1,
created: Mon Oct 17 2016 08:15:58 GMT+0700 (SE Asia Standard Time),
modified: Thu Nov 17 2016 10:59:00 GMT+0700 (SE Asia Standard Time),
salt: '140996000870',
Balance: 0,
ExpireDate: Mon Apr 02 2018 00:00:00 GMT+0700 (SE Asia Standard Time),
used: 0,
donate: 1 },
_previousAttributes:
{ id: 18,
first_name: 'test',
last_name: 'test',
email: 'test1@gmail.com',
password: '18fa9883b74578494b578048273e66d2edbf57b6',
active: 1,
created: Mon Oct 17 2016 08:15:58 GMT+0700 (SE Asia Standard Time),
modified: Thu Nov 17 2016 10:59:00 GMT+0700 (SE Asia Standard Time),
salt: '140996000870',
Balance: 0,
ExpireDate: Mon Apr 02 2018 00:00:00 GMT+0700 (SE Asia Standard Time),
used: 0,
donate: 1 },
changed: {},
relations: {},
cid: 'c1',
id: 18,
_knex: null }
18
undefined
答案 0 :(得分:2)
因为在提取的JSON中email
不是顶级键。它在attributes
键内。试试console.log(req.user.attributes.email);
。
答案 1 :(得分:1)
使用“req.user.attributes.email”代替“req.user.email”
答案 2 :(得分:1)
我有同样的问题,我想迭代用户对象的所有属性。
原来 req.user 对象不正常,属性实际上在 req.user._doc
内所以你可以迭代这样的属性:
Object.keys(req.user._doc).forEach(function(each_attribute){
console.log('each_attribute', each_attribute)
})
我希望它有所帮助。