为什么 arr 数组在创建后不在用户对象中? [英] Why is the arr array not in the users object after creation of it?

查看:27
本文介绍了为什么 arr 数组在创建后不在用户对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

客户

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

     }  
   }
 });

服务器/用户.js

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

推荐答案

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

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. arr 放在 profile 对象中.
  2. Accounts.onCreateUser回调中向用户添加arr.
  1. Put arr inside of the profile object.
  2. Add arr to the user in the Accounts.onCreateUser callback.

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

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

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

这篇关于为什么 arr 数组在创建后不在用户对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆