Stormpath Express:保存customData

我运行express-stormpath身份validation快速服务器并存储有关用户的相同的自定义数据。

如何发布数据到服务器并将其保存到stormpath? 目前我的post是这样的:

app.post('/post', stormpath.loginRequired, function(req, res) { var stundenplan_data = req.body; console.log(stundenplan_data); req.user.customData.stundenplan = stundenplan_data; req.user.customData.save(); }); 

我得到正确的数据,我想发布在console.log,但如果我在另一个请求中调用数据自定义数据是空的。

我是express-stormpath图书馆的作者,我会做的是这样的:

将Stormpath初始化为中间件时,添加以下设置以自动使customData可用:

 app.use(stormpath.init(app, { ..., expandCustomData: true, // this will help you out })); 

修改您的路线代码看起来像这样:

 app.post('/post', stormpath.loginRequired, function(req, res, next) { var studentPlan = req.body; console.log(studentPlan); req.user.customData.studentPlan = studentPlan; req.user.customData.save(function(err) { if (err) { next(err); // this will throw an error if something breaks when you try to save your changes } else { res.send('success!'); } }); }); 

您的更改没有在上面工作的原因是您没有首先展开customData。 Stormpath需要一个单独的请求来“抢”你的customData,所以如果你不这样做,事情将无法保存。

以上更改可以确保这种情况自动发生=)