1

我可以有一个 JSON 模板自动分配给 Stormpath 中每个新创建用户的自定义数据字段吗?

目前我正在尝试将我的 JSON 模板复制到 postRegistrationHandler 函数中的 account.customData ,但我正在努力正确地复制它。

所以我有...

 postRegistrationHandler: function (account, req, res, next) {
    console.log('User:', account.email, 'just registered!');
    writeCustomDataToAccount(account, customDataBlank);
    next();
},

'customDataBlank' 是服务器上的 .json 文件。接着...

var writeCustomDataToAccount = function (account, customData) {
account.getCustomData(function (err, data) {
    for (var field in customData) {
        data[field] = customData[field];
    }
    data.save();
});

}

这看起来合乎情理吗?

编辑: 好吧,我现在能够比以前更好地复制我的 JSON,但我的问题仍然存在 -我可以有一个 JSON 模板自动分配给 Stormpath 中每个新创建用户的自定义数据字段吗?

4

1 回答 1

0

您的代码对我来说是正确的——我是 express-stormpath 库的作者。您的代码将postRegistrationHandler自动为每个新创建的用户存储一些自定义数据。

但是,我确实注意到了一件不正常的事情——如果你的文件在磁盘上,它看起来不像是在任何地方加载它。我要做的是:

app.use(stormpath.init(app, {
  postRegistrationHandler: function(account, req, res, next) {
    console.log('User:', account.email, 'just registered!');
    account.getCustomData(function(err, data) {
      if (err) return next(err);

      var dataToStore = require(customDataBlank); // this will load JSON from the file on disk
      for (var field in require(customDataBlank)) {
        data[field] = dataToStore[field];
      }
      data.save(function(err) {
        if (err) return next(err);
        next();
      });
    });
  },
}));
于 2015-07-17T04:29:39.883 回答