Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections 发布/订阅同一集合的多个字段选择_Collections_Meteor_Publish Subscribe - Fatal编程技术网

Collections 发布/订阅同一集合的多个字段选择

Collections 发布/订阅同一集合的多个字段选择,collections,meteor,publish-subscribe,Collections,Meteor,Publish Subscribe,我有一个帖子集,其中包含一个内嵌的评论数组。 在每个帖子查看页面中,我需要发布两个游标:一个包含所有帖子,但没有在某些小部件中显示其评论字段(性能),另一个包含所选帖子及其评论。服务器端代码如下所示: Meteor.publish('allPosts', function() { return Posts.find({}, {fields: {'comments':0}}) }) Meteor.publish('singlePost', function(slug) { return P

我有一个帖子集,其中包含一个内嵌的评论数组。 在每个帖子查看页面中,我需要发布两个游标:一个包含所有帖子,但没有在某些小部件中显示其评论字段(性能),另一个包含所选帖子及其评论。服务器端代码如下所示:

Meteor.publish('allPosts', function() {
  return Posts.find({}, {fields: {'comments':0}})
})
Meteor.publish('singlePost', function(slug) {
  return Posts.find({slug: slug})
})
在post view模板中,我订阅了这两个,但当我使用

Posts.findOne({slug: slug})

我如何知道使用的是什么?如何选择一个?

当您在客户端上查询
帖子时,您正在查询该集合上所有活动订阅的联合。在这种情况下,如果您使用特定的
slug
订阅了
singlePost
,您将获得完整的文档。如果你没有,你将不会得到评论

另一个例子:

Meteor.publish(“所有摘要”,函数(){ 返回帖子。查找({},{字段:{标题:1,日期:1}); }); Meteor.publish(“myPosts”,函数(){ return Posts.find({creator:this.userId},{fields:{title:1,rating:1}}); }); Meteor.publish(“singlePost”,函数(_id){ return Posts.find({u id:{u id},{fields:{title:1,body:1}); });
假设一个用户id为“我”的客户端订阅了
allSummaries
myPosts
singlePost
(id为“3”)。该集合包含以下文档:

{u id:“1”,标题:“Post 1”,日期:“昨天”,创建者:“其他人”,评级:3}
{id:“2”,标题:“Post2”,日期:“today”,创建者:“me”,评级:4}
{u id:“3”,标题:“Post 3”,日期:“5天前”,创建者:“其他人”,评级:2}

对于Post 1,客户端将只看到
标题
日期
(和
\u id
)-发布于
所有摘要
-除此之外没有其他内容。对于第2篇文章,他们将看到
标题
日期
,和
评级
日期
来自
所有摘要
标题
来自
所有摘要
我的帖子
,以及
评级
来自
我的帖子
在第三篇文章中,他们会看到
标题
日期
正文
,但不会看到
评级

“所有活动订阅的联合”,就是这样!但在文档中没有提到,比我在其他地方能找到的要清楚得多。向上投票!