2014-11-14 73 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