2

在使用 Express-Stormpath 模块通过 node/express 将用户注册到 Stormpath 后,我遇到了一个奇怪的错误(至少在我看来)。

刚注册后,我似乎无法在第一个用户会话的各种路线中访问 c​​ustomData。在注册时,我正在为发票创建和排列

app.use(stormpath.init(app, {
    ...,
    expandCustomData: true,
    postRegistrationHandler: function(account, req, res, next) {

        account.customData.invoices = [];
        account.save();

        next();
    }
}));

但是当我在我的索引路径中访问它们时,我得到了这个错误

router.get('/', stormpath.loginRequired, function(req, res){

    console.log(req.user.customData.invoices ); // undefined

     res.render('index', {
        title: 'Index'
     });
});

如果我杀死我的本地并重新启动它,我会怎么做

 console.log(req.user.customData.invoices ); // []

这就是我想要的。

谁能阐明我在这里做错了什么?

提前谢谢。

4

1 回答 1

3

这里发生的情况是:当您在postRegistrationHandler代码中时—— customData 默认情况下不会自动可用。注册后立即postRegistrationHandler调用,然后任何辅助函数开始生效。

要使您的示例正常工作,您需要首先从 Stormpath 服务中“获取”customData。

这是一个工作示例:

app.use(stormpath.init(app, {
  ...,
  expandCustomData: true,
  postRegistrationHandler: function(account, req, res, next) {
    account.getCustomData(function(err, data) {
      if (err) return next(err);
      data.invoices = [];
      data.save();
      next();
    });
  }
}));

上面的问题在文档中真的不清楚——这是我 100% 的错(我是图书馆的作者)——我今天会解决这个问题=)

于 2015-05-19T17:05:02.357 回答