Accounts.onCreateUser在创建新用户时添加额外属性,良好做法?

leh*_*htu 10 meteor meteor-accounts

我正在使用Accounts.createUser()创建新用户,如果您没有做任何花哨的事情,它会正常工作.但我想向新用户添加一些未在文档中列出的其他字段.这是我的代码:

var options = {
    username: "funnyUserNameHere",
    email: "username@liamg.com",
    password: "drowssap",
    profile: {
        name: "Real Name"
    },
    secretAttribute: "secretString"
};

var userId = Accounts.createUser(options);
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我已将secretAttribute添加到我的选项对象中.因为没有记录,所以不公平地说它不是在用户对象下添加我的属性.

所以我用谷歌搜索并发现这样的东西可能会起作用:

Accounts.onCreateUser(function(options, user) {
    if (options.secretAttribute)
        user.secretAttribute = options.secretAttribute;

    return user;
});
Run Code Online (Sandbox Code Playgroud)

是的!这是有效的,但总有BUTT ..*但是......在这之后,它不再在用户对象下保存配置文件.然而,这使它工作:

Accounts.onCreateUser(function(options, user) {
    if (options.secretAttribute)
        user.secretAttribute = options.secretAttribute;

    if (options.profile)
        user.profile = options.profile;

    return user;
});
Run Code Online (Sandbox Code Playgroud)

那么我想要你们呢?

  1. 我想知道为什么onCreateUser在我的情况下丢失了配置文件(在上面的修复之前)?
  2. 我的做法是好的做法吗?
  3. 是否有更好的解决方案在创建用户对象时为其添加额外属性?

ps:我认为很明显为什么我不想保存配置文件下的所有额外字段;)

leh*_*htu 5

好吧,它不是那么难.它在文档中说:"默认的创建用户函数只是将options.profile复制到新的用户文档中.调用onCreateUser会覆盖默认的钩子." - Accounts.onCreateUser