访问Meteor.users的服务字段

时间:2014-10-30 06:06:21

标签: mongodb meteor

当我查询Meteor.users时,我没有收到services字段或我在profile之外创建的任何其他自定义字段。为什么我只在客户端上收到_idprofile,如何才能收到整个Meteor.users对象?

感谢。

2 个答案:

答案 0 :(得分:3)

来自DOcs

默认情况下,当前用户的用户名,电子邮件和个人资料会发布到客户端。您可以使用以下命令为当前用户发布其他字段:

如上所述如果您需要其他字段,则需要发布它们

// server

Meteor.publish("userData", function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'services': 1, 'others': 1}});
  } else {
    this.ready();
  }
});

//客户端

Meteor.subscribe("userData");

答案 1 :(得分:0)

上述答案确实有效,但这意味着您必须订阅所述数据,如果您从当前登录的用户那里获取数据,则应该这样做。

但是,如果您关心的只是登录用户的数据,那么您可以使用null发布来获取数据而无需订阅。

在服务器上执行,

Meteor.publish(null, function () {
  if (! this.userId) {
    return null;
  }

  return Meteor.users.find(this.userId, {
    fields: {
      services: 1,
      profile: 1,
      roles: 1,
      username: 1,
    },
  });
});

这实际上是帐户包的作用under the hood