Why is the arr array not in the users object after creation of it?

我的梦境 提交于 2020-01-15 12:15:13

问题


I am try to make arr a array that every user has but is never sent to client side. A day ago the it stopped being put into user objects on user create. Here is the code; thanks.

client

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
        }
      });

     }  
   }
 });

server/user.js

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

回答1:


Accounts.createUser takes an object with at most 4 fields: username, email, password, and profile. You are passing in arr, which is being ignored by the server. You have two options:

  1. Put arr inside of the profile object.
  2. Add arr to the user in the Accounts.onCreateUser callback.

option 1:

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

option 2:

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

In that case, you will also need to publish the extra field so that the client can see it. See the users section of the docs. Specifically:

// 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");


来源:https://stackoverflow.com/questions/26821511/why-is-the-arr-array-not-in-the-users-object-after-creation-of-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!