本文介绍了订阅 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') 它起作用并返回 undefined 我敢打赌它会在服务器上创建一个我没有的 mongo 集合就像因为 USER 集合已经存在并且它指向一个新的集合而不是 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 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 02:04
查看更多