订阅Meteor.Users集合 [英] Subscribing to Meteor.Users Collection

查看:55
本文介绍了订阅Meteor.Users集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// in server.js
Meteor.publish("directory", function () {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});

// in client.js
Meteor.subscribe("directory");

我现在想从浏览器的控制台获取从客户端查询的目录列表,例如directory.findOne(). //测试目的

I want to now get the directory listings queried from the client like directory.findOne() from the browser's console. //Testing purposes

执行directory=Meteor.subscribe('directory')/directory=Meteor.Collection('directory')和执行directory.findOne()不起作用,但是当我执行directory=new Meteor.Collection('directory')时,它起作用并返回未定义,我敢打赌它会在服务器上创建一个我不喜欢的mongo集合,因为USER集合已经存在,它指向一个新的Collection,而不是USER集合.

Doing directory=Meteor.subscribe('directory')/directory=Meteor.Collection('directory') and performing directory.findOne() doesn't work but when I do directory=new Meteor.Collection('directory') it works and returns undefined and I bet it CREATES a mongo collection on the server which I don't like because USER collection already exists and it points to a new Collection rather than the USER collection.

注意:我不想弄混Meteor.users集合如何处理其功能...我只是想使用不同的句柄从中检索一些特定数据,该句柄只会返回指定的字段,而不会覆盖其默认值功能...

NOTE: I don't wanna mess with how Meteor.users collection handles its function... I just want to retrieve some specific data from it using a different handle that will only return the specified fields and not to override its default function...

例如:

Meteor.users.findOne() // will return the currentLoggedIn users data
directory.findOne() // will return different fields taken from Meteor.users collection. 

推荐答案

如果您希望此设置有效,则需要执行以下操作:

If you want this setup to work, you need to do the following:

Meteor.publish('thisNameDoesNotMatter', function () {
  var self = this;
  var handle = Meteor.users.find({}, {
    fields: {emails: 1, profile: 1}
  }).observeChanges({
    added: function (id, fields) {
      self.added('thisNameMatters', id, fields);
    },
    changed: function (id, fields) {
      self.changed('thisNameMatters', id, fields);
    },
    removed: function (id) {
      self.removed('thisNameMatters', id);
    }
  });

  self.ready();

  self.onStop(function () {
    handle.stop();
  });

});

否,您需要在客户端上定义仅客户端集合:

No on the client side you need to define a client-side-only collection:

directories = new Meteor.Collection('thisNameMatters');

并订阅相应的数据集:

Meteor.subscribe('thisNameDoesNotMatter');

现在应该可以使用了.如果您认为这种解释不够清楚,请告诉我.

This should work now. Let me know if you think this explanation is not clear enough.

编辑

在这里,self.added/changed/removed方法或多或少地充当事件分发程序.简而言之,他们会向每位致电的客户提供指导

Here, the self.added/changed/removed methods act more or less as an event dispatcher. Briefly speaking they give instructions to every client who called

Meteor.subscribe('thisNameDoesNotMatter');

关于应该在名为thisNameMatters的客户端集合上应用的更新(假定此集合存在).名称-作为第一个参数传递的名称-几乎可以任意选择,但是如果客户端上没有相应的集合,则所有更新都将被忽略.请注意,此集合只能是客户端的,因此它不一定必须与数据库中的真实"集合相对应.

about the updates that should be applied on the client's collection named thisNameMatters assuming that this collection exists. The name - passed as the first parameter - can be chosen almost arbitrarily, but if there's no corresponding collection on the client side all the updates will be ignored. Note that this collection can be client-side-only, so it does not necessarily have to correspond to a "real" collection in your database.

publish方法返回游标,它只是上述代码的快捷方式,唯一的区别是使用实际集合的名称而不是我们的theNameMatters.实际上,此机制允许您根据需要创建尽可能多的数据集镜像".在某些情况下,这可能非常有用.唯一的问题是这些集合"将是只读的(顺便说一句,顺便说一句),因为如果未在服务器上定义它们,则相应的插入/更新/删除"方法将不存在.

Returning a cursor from your publish method it's only a shortcut for the above code, with the only difference that the name of an actual collection is used instead of our theNameMatters. This mechanism actually allows you to create as many "mirrors" of your datasets as you wish. In some situations this might be quite useful. The only problem is that these "collections" will be read-only (which totally make sense BTW) because if they're not defined on the server the corresponding `insert/update/remove' methods do not exist.

这篇关于订阅Meteor.Users集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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