2016-11-18 68 views
0

我正在使用Passport授權我的項目。成功驗證後,用戶被定向到「/home」。此時,請求具有用戶對象。但是,一旦我重定向,req.user.email是未定義的。 :'(無法獲取req.user的屬性

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: '[email protected]', 
     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: '[email protected]', 
     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 

回答

2

因爲在JSON正在取出email不是頂級密鑰及其內attributes鍵與console.log(req.user.attributes.email);嘗試

+1

這是工作謝謝 – beginerdeveloper

1

使用「。 req.user.attributes.email「而不是」req.user.email「

1

我有同樣的問題,我想迭代你的所有屬性ser對象。

原來,req.user對象不是一個正常的,並且屬性實際上都在裏面req.user._doc

所以,你可以在這樣的屬性迭代:

Object.keys(req.user._doc).forEach(function(each_attribute){ 
    console.log('each_attribute', each_attribute) 
}) 

我希望它有幫助。