繁体   English   中英

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

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

我想向Meteor的用户集合中添加一个新字段:

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

但是当我做Meteor.users.find().fetch();

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

我没看到田野。

在模板中不起作用,或者:

文件/document_page.js:

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

    return users
  },

文件/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>

仅显示用户名。 可能是什么问题呢?

您需要发布自定义字段。 从文档:

默认情况下,服务器发布用户名,电子邮件和配置文件(可由用户写入)。 有关用户文档中使用的字段的更多信息,请参见Meteor.users。

最简单的方法是使用名称为null发布,它会自动发布文档,而无需相应的Meteor.subscribe

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

在这种情况下,我们将发布role字段,该role字段将与为当前用户已经发布的默认字段合并。

暂无
暂无

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

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