为什么arr数组在创建后不在user对象中?

时间:2014-11-08 20:03:26

标签: mongodb meteor

我试图让arr成为每个用户都拥有但从未发送到客户端的数组。前一天,它停止在用户创建时放入用户对象。这是代码;感谢。

客户

Template.create_user.events({
 'click #create-user-button': function() {
    var username = $("#username").val();
    var password = $("#password").val();
    var email = $("#email").val();
    var bio = $("#bio").val() || "";
    if (!username || !password || !email) {
    } else {
      Accounts.createUser({
        username: username,
        password: password,
        email: email,
        arr:[],
        profile: {
            bio: bio
        }
      });

     }  
   }
 });

服务器/ user.js的

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

1 个答案:

答案 0 :(得分:2)

Accounts.createUser获取最多4个字段的对象:用户名,电子邮件,密码和个人资料。您正在传递arr,服务器会忽略它。您有两种选择:

  1. arr放在profile对象内。
  2. arr回调中向用户添加Accounts.onCreateUser
  3. 选项1:

    Accounts.createUser({
      username: username,
      password: password,
      email: email,
      profile: {
          bio: bio,
          arr: []
      }
    });
    

    选项2:

    Accounts.onCreateUser(function(options, user) {
      if (options.profile)
        user.profile = options.profile;
      user.arr = [];
      return user;
    });
    

    在这种情况下,您还需要发布额外字段,以便客户端可以看到它。请参阅文档的users部分。具体做法是:

    // server
    Meteor.publish("userData", function () {
      if (this.userId) {
        return Meteor.users.find({_id: this.userId}, {fields: {arr: 1}});
      } else {
        this.ready();
      }
    });
    
    // client
    Meteor.subscribe("userData");