简体   繁体   English

Accounts.onCreateUser未将字段添加到流星的用户集合

[英]Accounts.onCreateUser not adding field to Meteor's user collection

I want to add a new field to Meteor's user collection: 我想向Meteor的用户集合中添加一个新字段:

server/fixtures.js: server / fixtures.js:

Accounts.onCreateUser(function(options, user) {
  user.role = 'Student'
  // We still want the default hook's 'profile' behavior.
  if (options.profile)
    user.profile = options.profile;
  return user
})

But when I do Meteor.users.find().fetch(); 但是当我做Meteor.users.find().fetch(); :

Object
_id: "7zLDKQE4ACJCfeEhr"
username: "alex"
__proto__: Object

I don't see the field. 我没看到田野。

Isn't working in the template, either: 在模板中不起作用,或者:

documents/document_page.js: 文件/document_page.js:

  users: function() {
    var users = _.map(Meteor.presences.find().fetch(), function(user) { 
      return Meteor.users.findOne({_id: user.userId})
    })

    return users
  },

documents/user.html 文件/user.html

<template name="user">
  <li class="clearfix">
    <img src="/avatar.png"/>
    <div class="user-info">
      <p>{{userId}}</p>
      <p>{{username}}</p>
      <p>{{role}}</p>
    </div>
  </li>
</template>

Only username is showing. 仅显示用户名。 What could be the problem? 可能是什么问题呢?

You need to publish the custom fields. 您需要发布自定义字段。 From the docs: 从文档:

By default the server publishes username, emails, and profile (writable by user). 默认情况下,服务器发布用户名,电子邮件和配置文件(可由用户写入)。 See Meteor.users for more on the fields used in user documents. 有关用户文档中使用的字段的更多信息,请参见Meteor.users。

The easiest way to do this is just to publish with a name of null which will automatically publish the documents without the need for a corresponding Meteor.subscribe : 最简单的方法是使用名称为null发布,它会自动发布文档,而无需相应的Meteor.subscribe

Meteor.publish(null, function() {
  return Meteor.users.find(this.userId, {fields: {role: 1}});
});

In this case we are publishing the role field which will be merged with the default fields already published for the current user. 在这种情况下,我们将发布role字段,该role字段将与为当前用户已经发布的默认字段合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM