对于未login的用户,无法访问customData – Express.js Stormpath

以下教程:

A Simple Web App With Node.js, Express, Bootstrap & Stormpath

我可以轻松访问和显示在profile.jade ,customData为login用户,从profile.js与显示

username: req.user.username, city: req.user.customData.city, 

文件名:profile.jade

 h1 This is surname #{username} and #{city} 

我想在一个视图中显示一个非login用户的数据,我的url为: http:// localhost:3000 / -jsmith ,但我无法访问任何customData。

FILENAME:server.js我有这个设置。

 app.use(stormpath.init(app, { expand: { customData: true 

 app.get('/-:id', function(req, res, next) { console.log('the response will be sent by the next function ...'); var id = req.params.id; next(); }, function(req, res) { var id = req.params.id; req.app.get('stormpathApplication').getAccounts({ username: id }, (err, accounts) => { if (err) throw err; accounts.each((account, cb) => { console.log('Found matching account:', account); cb(); res.render('user', { email: account.email, city: account.customData.city, }); }); }); } ); 

问题:

文件名:user.jade

用户名显示,但没有为城市

 h1 This is surname #{username} and #{city} 

我哪里错了?

如果直接使用Stormpath Application对象并使用它进行API调用,则实际上需要为每个API调用添加{ expand: 'customData' }参数。

我知道你已经为你的express-stormpathconfiguration设置了它 – 这只适用于应用程序为你的API调用(例如req.user)。 我可以看到这似乎令人困惑。

所以要做到这一点,只需更换:

  req.app.get('stormpathApplication').getAccounts({ username: id } 

附:

  req.app.get('stormpathApplication').getAccounts({ username: id, expand: 'customData' } 

而事情应该工作。 快乐的编码!