【问题标题】:Why is the arr array not in the users object after creation of it?为什么 arr 数组在创建后不在 users 对象中?
【发布时间】:2014-11-09 04:59:23
【问题描述】:

我试图使 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;
});

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

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

    1. arr 放在profile 对象内。
    2. Accounts.onCreateUser 回调中为用户添加arr

    选项 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");
    

    【讨论】:

    • 这些 API 已经很久没有改变了,所以除了胡乱猜测之外,我没有太多可以提供的。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多