2014-11-14 76 views
4

我正在运行express-stormpath auth的快速服务器并存储有关用户的相同自定义数据。Stormpath Express:保存自定义数据

如何发布数据到服务器并将它们保存为stormpath? 目前我的帖子是这样的:

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后,但如果我叫在另一个get请求的数据自定义数据是空的正确的数据。

+0

你可以传递一个回调来保存()函数,看它是否返回任何错误? – robertjd 2014-11-14 22:21:46

+0

没有IAM没有得到unsing当任何错误:res.locals.user.save(函数(ERR,updatedUser){ \t \t如果(ERR){ \t \t updatedUser.customData.anotherfield; \t \t的console.log(! “error”); // undefined \t \t} \t}); – brighthero 2014-11-15 10:18:33

回答

4

我是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,所以如果你不这样做,事情将无法保存。

上述变化,确保自动发生这种情况你=)

+0

非常感谢!我希望你能回答我! :d – brighthero 2014-11-19 18:21:24