我有 3 个集合meteor.user()
,Categories
和Posts
。
用户可以通过将类别的 id 保存在数组中来选择关注任何类别的帖子meteor.user()
这样做的最终目标是用户拥有一个个人信息流,其中仅显示他们关注的类别中的帖子。
里面的每个帖子Posts collection
都有一个包含它的类别的数组。
每个类别都有一个帖子数组,数组中的每个帖子都有一个帖子字段ID
。
whereid: 67
是类别的 id,是类别posts.ID: 74
中的帖子的 id。
下面是 Posts 集合中帖子的 ID,与类别集合中的 posts.ID 匹配
用户保存一个类别 id,因此它在meteor.user() 中显示为类别数组
当我这样做时
Template.userTimeline.helpers({
category: function() {
var posts = Category.find({
id: {
$in: Meteor.user().category
}
});
return posts
console.log(posts);
}
});
我可以做这个
{{#each category}}
<ul class="article-timeline">
{{#each posts}} {{ID}} {{/each}}
</ul>
{{/each}}
但这让我可以访问类别集合中的 posts.ID 并显示它。
问题
我怎样才能走得更远,将我用上面的代码得到的posts.ID匹配到一个帖子的id,Post
这样我就可以访问Posts
集合中的帖子。我可以做这样的事情吗?
Template.userTimeline.helpers({
category: function() {
var postsInCategory = Category.find({
id: {
$in: Meteor.user().category
}
});
var postMatch = Post.find({
ID: {
$in: postsInCategory
}
});
return postMatch
console.log(postMatch);
}
});
ZIM 回答后的进一步编辑
因此,您给我的解决方案是从我的收藏中包含post.title
的 post 对象中获取,如下所示。Categories
中的这个post
数组Category collection
是不完整的,它是一个原始的副本Posts Collection
我需要做的是使用post.ID
如上图所示。即类别集合中的帖子数组,以创建与原始集合中的帖子的多对多关系,而不是让我需要如下所示。id
Posts
post.title
title.rendered
帖子集合。
如果您看到ID
(不是顶部 id,它是类别的 id),则Categories集合中post.ID
的帖子数组中的帖子与集合中的帖子相同。这是这一切的关键,帮助我创建一个关系,这样,我需要在原始集合中显示,而不是显示或来自集合中的帖子数组。id
Posts
title
post.title
Categories
title.rendered
Posts