Accounts.onCreateUser 在创建新用户时添加额外的属性,好的做法? [英] Accounts.onCreateUser adding extra attributes while creating new users, good practices?
问题描述
我正在使用 Accounts.createUser() 创建新用户并且它正常工作如果你没有做任何花哨的事情.但我想向新用户添加一些未在文档中列出的其他字段.这是我的代码:
I'm creating new user with Accounts.createUser() and it works normally if you are not doing anything fancy. But I want to add some other fields to new user that are not listed on documentation. Here is my code:
var options = {
username: "funnyUserNameHere",
email: "username@liamg.com",
password: "drowssap",
profile: {
name: "Real Name"
},
secretAttribute: "secretString"
};
var userId = Accounts.createUser(options);
在此示例中,我已将 secretAttribute 添加到我的选项对象.因为这没有记录,所以它没有在用户对象下添加我的属性是公平的.
In this example I have added secretAttribute to my options object. Because this is not documented it's just fair it's not adding my attribute under user object.
所以我用谷歌搜索并发现这样的事情可能有效:
So I googled and figured out that something like this might work:
Accounts.onCreateUser(function(options, user) {
if (options.secretAttribute)
user.secretAttribute = options.secretAttribute;
return user;
});
是的!这有效,但总是有 BUTT.. *BUT.. 在这之后它不再在用户对象下保存 profile .但是,这使它起作用:
And yes! This works, but there is always BUTT.. *BUT.. After this one it's not saving profile anymore under the user object. However this makes it work:
Accounts.onCreateUser(function(options, user) {
if (options.secretAttribute)
user.secretAttribute = options.secretAttribute;
if (options.profile)
user.profile = options.profile;
return user;
});
那么我想从你们那里得到什么?
So what I want from you guys?
- 我想知道为什么 onCreateUser 会丢失我的个人资料(在上述修复之前)?
- 我的方法是好的做法吗?
- 是否有更好的解决方案在创建用户对象时为它们添加额外的属性?
ps:我认为很明显为什么我不想在配置文件下保存所有额外的字段;)
ps: I thinks it's obvious why I don't want to save all extra fields under profile ;)
推荐答案
嗯,没那么难.. 这里是文档中的内容:默认的创建用户函数只是将 options.profile 复制到新的用户文档中.调用onCreateUser 覆盖默认钩子."- Accounts.onCreateUser
Well it wasn't so hard.. Here it stands in documentation: "The default create user function simply copies options.profile into the new user document. Calling onCreateUser overrides the default hook." - Accounts.onCreateUser
这篇关于Accounts.onCreateUser 在创建新用户时添加额外的属性,好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!