Node.js-Express-Strompath 注册后立即没有自定义数据
Node.js-Express-Strompath No CustomData after immediately after registration
在使用 Express-Stormpath 模块通过 node/express 将用户注册到 stormpath 后,我遇到了一个奇怪的错误(至少在我看来)。
刚注册后,我似乎无法在第一个用户会话中访问各种路由中的 customData。注册时我正在为发票创建和排列
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 ); // []
这是我想要的。
任何人都可以阐明我在这里做错了什么吗?
提前致谢。
这里发生的事情是这样的:当您在 postRegistrationHandler
代码中时 -- 默认情况下 customData 不会自动可用。 postRegistrationHandler
在注册后 立即被调用 在任何辅助函数发挥作用之前。
要使您的示例正常工作,您首先需要做的是 'fetch' 来自 Stormpath 服务的自定义数据。
这是一个工作示例:
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% 是我的错(我是库的作者)——我会解决这个问题今天 =)
在使用 Express-Stormpath 模块通过 node/express 将用户注册到 stormpath 后,我遇到了一个奇怪的错误(至少在我看来)。
刚注册后,我似乎无法在第一个用户会话中访问各种路由中的 customData。注册时我正在为发票创建和排列
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 ); // []
这是我想要的。
任何人都可以阐明我在这里做错了什么吗?
提前致谢。
这里发生的事情是这样的:当您在 postRegistrationHandler
代码中时 -- 默认情况下 customData 不会自动可用。 postRegistrationHandler
在注册后 立即被调用 在任何辅助函数发挥作用之前。
要使您的示例正常工作,您首先需要做的是 'fetch' 来自 Stormpath 服务的自定义数据。
这是一个工作示例:
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% 是我的错(我是库的作者)——我会解决这个问题今天 =)