Node.js Stormpath Express:保存自定义数据
我正在运行一个带有express stormpath for auth的express服务器,以存储关于用户的相同自定义数据 如何将数据发布到服务器并保存到stormpath? 目前我的帖子如下所示:Node.js Stormpath Express:保存自定义数据,node.js,express,stormpath,Node.js,Express,Stormpath,我正在运行一个带有express stormpath for auth的express服务器,以存储关于用户的相同自定义数据 如何将数据发布到服务器并保存到stormpath? 目前我的帖子如下所示: app.post('/post', stormpath.loginRequired, function(req, res) { var stundenplan_data = req.body; console.log(stundenplan_data); req.user.cus
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();
});
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!');
}
});
});
我正在获取要在console.log中发布的正确数据,但如果我在另一个get请求中调用该数据,则自定义数据为空。我是
express stormpath
库的作者,我要做的是:
将Stormpath初始化为中间件时,添加以下设置以自动使customData可用:
app.use(stormpath.init(app, {
...,
expandCustomData: true, // this will help you out
}));
修改路线代码,如下所示:
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();
});
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,因此如果您不先这样做,则无法保存
上面的更改可确保您自动执行此操作=)您可以将回调传递给save()函数,并查看它是否返回任何错误吗?没有iam在取消以下操作时没有收到任何错误:res.locals.user.save(函数(err,updateuser){if(!err){updateuser.customData.anotherfield;console.log(“错误”);//未定义的});非常感谢你!我希望你能回答我!:D