【问题标题】:Meteor add additional fields to user account at root levelMeteor 在根级别向用户帐户添加其他字段
【发布时间】:2015-02-12 02:27:37
【问题描述】:

如何将其他字段添加到用户集合中。我了解选项对象允许四个字段 - 用户名、电子邮件密码和个人资料。所以在 Accounts.onCreateUser 上, 有没有办法在根级别(不在配置文件字段内)添加其他字段?

到目前为止,解决方案是使用 Accounts.createUser 在配置文件中添加字段,将此字段复制到根级别,然后使用 Accounts.onCreateUser 删除配置文件中的字段。这是在我下面的示例中为“userType”完成的

客户端.js

Template.joinForm.events({
'submit .form-join': function(e, t) {
    e.preventDefault();
    var firstName = t.find('#firstName').value,
    lastName = t.find('#lastName').value,
    email = t.find('#email').value,
    password = t.find('#password').value,
    username = firstName + '.' + lastName,
    profile = {
            name: firstName + ' ' + lastName,
            userType: selectedUserType // this is copied to root level and deleted from profile.
};

    Accounts.createUser({
        //NEWFIELD1: [],
        //NEWFILED2: [],
        email: email,
        username: username,
        password: password,
        profile: profile
    }, function(error) {
        if (error) {
            alert(error);
        } else {
            Router.go('/');
        }
    });
}
});

server.js

Accounts.onCreateUser(function(options, user) {
if (options.profile) {
if (options.profile.userType) {
  user.userType = options.profile.userType;
  delete options.profile.userType;
}
user.profile = options.profile;
}
return user;
});

【问题讨论】:

    标签: meteor createuser


    【解决方案1】:

    设置字段的唯一其他方法是在通过方法调用创建帐户后更新用户文档。例如:

    客户

    var extraFields = {
      newField1: 'foo',
      newField2: 'bar'
    };
    
    Accounts.createUser(..., function(err1) {
      if (err1) {
        alert(err1);
      } else {
        Meteor.call('setUserFields', extraFields, function(err2) {
          if (err2) {
            alert(err2);
          } else {
            Router.go('/');
          }
        });
      }
    });
    

    服务器

    Meteor.methods({
      setUserFields: function(extraFields) {
        // TODO: change this check to match your user schema
        check(extraFields, {
          newField1: Match.Optional(String),
          newField2: Match.Optional(String)
        });
    
        return Meteor.users.update(this.userId, {$set: extraFields});
      }
    });
    

    这种方法的主要问题是用户可以随时打开控制台并调用setUserFields 方法。这可能是也可能不是问题,具体取决于您的用例。如有必要,您始终可以向该方法添加额外的检查以防止后续更新。

    【讨论】:

    • 这是一个可行的解决方案。感谢您指出安全问题。对于我的用例,我不希望用户拥有控制权。我可能会求助于在配置文件中保留其他字段。
    【解决方案2】:

    我已经能够在 Accounts.onCreateUser 上创建没有任何值 (null) 的附加字段。

    请指出此解决方案的任何问题。 记得发布附加字段。

    server.js

    Accounts.onCreateUser(function(options, user) {
    if (options.profile) {
    if (options.profile.userType) {
      user.userType = options.profile.userType;
      delete options.profile.userType;
      user.newfieldone = options.newfieldone; //this is the line to insert new field
      user.newfieldtwo = options.newfieldtwo;
    }
    user.profile = options.profile;
    }
    return user;
    });
    
    
    Meteor.publish(null, function() {
    // automatically publish the userType for the connected user
    // no subscription is necessary
    return Meteor.users.find(this.userId, {fields: {newfieldone: 1, newfieldtwo: 1}});
    });
    

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多