我正在创建新用户Accounts.createUser() http://docs.meteor.com/#/full/accounts_createuser如果你不做任何花哨的事情,它就会正常工作。但我想向新用户添加一些文档中未列出的其他字段。这是我的代码:
var options = {
username: "funnyUserNameHere",
email: "[email protected] /cdn-cgi/l/email-protection",
password: "drowssap",
profile: {
name: "Real Name"
},
secretAttribute: "secretString"
};
var userId = Accounts.createUser(options);
在此示例中,我已将 SecretAttribute 添加到我的选项对象中。因为这没有记录,所以它没有在用户对象下添加我的属性是公平的。
所以我用谷歌搜索并发现类似这样的东西可能有效:
Accounts.onCreateUser(function(options, user) {
if (options.secretAttribute)
user.secretAttribute = options.secretAttribute;
return user;
});
是的!这可行,但总是有 BUTT.. *但是.. 在这之后它不会保存profile不再位于用户对象下。然而,这使它工作:
Accounts.onCreateUser(function(options, user) {
if (options.secretAttribute)
user.secretAttribute = options.secretAttribute;
if (options.profile)
user.profile = options.profile;
return user;
});
那么我想从你们那里得到什么?
- 我想知道为什么 onCreateUser 在我的情况下会丢失个人资料(在上述修复之前)?
- 我的方法是好的做法吗?
- 是否有更好的解决方案在创建用户对象时添加额外的属性?
ps:我认为这很明显为什么我不想保存个人资料下的所有额外字段;)