【发布时间】:2016-11-14 02:23:42
【问题描述】:
我有两个系列。一种称为帖子,另一种称为类别。在帖子集合中,每个帖子都包含一个名为 categories 的 ID 数组,这些 ID 是各个帖子所属类别的 ID,存储在帖子中。
第二个集合是 Categories 集合,其中包含每个帖子所属的类别
目标
在我的模板中,我将每个帖子显示为其标题、内容、图像和作者,以及通过将帖子集合中的类别 ID 链接到类别集合中的各个类别而产生的类别名称
<template name="latest">
{{#each posts}}
<div>{{> category}}</div>
<h5 class="latest-title">{{title.rendered}}</h5>
<img class="latest-img" src="{{featured_image_thumbnail_url}}" alt="" />
{{/each}}
</template>
在我的类别模板中
<template name="category">
{{#each categories}}
{{name}}
{{/each}}
</template>
在我的 category.js 中
Template.category.helpers({
categories(){
return CategoryCollection.find({ id: parseInt(this.categories) });
}
});
如您所见,我想显示属于帖子的类别的名称,它们位于一个数组中,因为帖子可能也有 3 个属于它的类别。但我似乎无法让它工作。
编辑
这是我的编辑,包括$in
Template.category.helpers({
categories(){
return CategoryCollection.find({ id: {$in: this.categories }});
}
});
这是我的模板
<template name="category">
{{#each categories}}
{{name}}
{{/each}}
</template>
它似乎不起作用。
进展
它不起作用,因为我没有为我的示例帖子分配类别,上面编辑的代码就是答案
【问题讨论】:
标签: javascript mongodb meteor mongo-collection