我如何才能显示 Collection B 中的项目字段,这些字段由 Collection A 中的项目的 id 引用?
How can i can display fields from an item from Collection B referenced by an id from an item from Collection A?
我有两个collections
CategoryCollection = new Mongo.Collection("CategoryCollection");
usersWordpressCollection = new Mongo.Collection("usersWordpressCollection");
CategoryCollection 在一个类别中保存 posts,在 posts objects 中有作者,它以 id 的形式出现。看看下面的图片,请注意 posts.author
usersWordpressCollection 包含 id(见下图)在 posts.author [=26= 下的 categoryCollection 中引用的作者]
当我显示 post 时,它来自 categoryCollection
,所以我可以获得 post、link 等。不过我也可以获得作者 ID说到这里,我想return,不是id,而是在usersWordpressCollection
中找到的引用作者信息
最新html
<template name="latest">
<ul>
{{#each articles}}
<li class=" home-latest ">
<span class="label"> <a class="" href="/catsingle/CategorySingle/{{_id}}">{{name}}</a></span>
{{#each posts}}
<div class="card">
<div class="category-img">{{{image}}}</div>
<div class="card-divider">
{{title}}
</div>
<div class="card-section">
<p>
{{> authors}}
</p>
</div>
</div>
</li>
{{/each}}
</ul>
</template>
最新js
Template.latest.helpers({
articles: function () {
var id = FlowRouter.getParam('_id');
return CategoryCollection.find({}, {sort: {date_created: -1}, limit:1}).fetch();
}
});
作者html
<template name="authors">
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
</template>
作者 js
Template.authors.helpers({
authors: function () {
var id = this.author;
console.log(id);
var alias = usersWordpressCollection.findOne(id, {fields: {name: 1} });
return alias.name;
console.log(alias.name);
});
如您所见,this.author
是在categoryCollection
posts.author
中找到的作者id。我试图在 usersWordpressCollection
中找到与作者匹配的 id,然后我想显示 id[= 作者的 name field
57=] 匹配。但我看不到让它工作,我该如何实现。
由于每个 post 只有一位作者,因此您无需遍历作者游标,您只需要一个。那么你在哪里:
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
改为使用:
{{#with author}}
<p>
{{name}}
</p>
{{/with}}
现在,进入 authors
模板的数据上下文将是一个 post
对象(因为您在 {{#each posts}}
中。因此您可以构建 author
助手如下:
Template.authors.helpers({
author(){
return usersWordpressCollection.findOne({ id: parseInt(this.author) });
}
});
既然您已经指出 this.author
是一个字符串,但您的用户集合的 id
是一个整数,那么您需要转换类型 - 您可以直接在查询中执行此操作。
我有两个collections
CategoryCollection = new Mongo.Collection("CategoryCollection");
usersWordpressCollection = new Mongo.Collection("usersWordpressCollection");
CategoryCollection 在一个类别中保存 posts,在 posts objects 中有作者,它以 id 的形式出现。看看下面的图片,请注意 posts.author
usersWordpressCollection 包含 id(见下图)在 posts.author [=26= 下的 categoryCollection 中引用的作者]
当我显示 post 时,它来自 categoryCollection
,所以我可以获得 post、link 等。不过我也可以获得作者 ID说到这里,我想return,不是id,而是在usersWordpressCollection
最新html
<template name="latest">
<ul>
{{#each articles}}
<li class=" home-latest ">
<span class="label"> <a class="" href="/catsingle/CategorySingle/{{_id}}">{{name}}</a></span>
{{#each posts}}
<div class="card">
<div class="category-img">{{{image}}}</div>
<div class="card-divider">
{{title}}
</div>
<div class="card-section">
<p>
{{> authors}}
</p>
</div>
</div>
</li>
{{/each}}
</ul>
</template>
最新js
Template.latest.helpers({
articles: function () {
var id = FlowRouter.getParam('_id');
return CategoryCollection.find({}, {sort: {date_created: -1}, limit:1}).fetch();
}
});
作者html
<template name="authors">
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
</template>
作者 js
Template.authors.helpers({
authors: function () {
var id = this.author;
console.log(id);
var alias = usersWordpressCollection.findOne(id, {fields: {name: 1} });
return alias.name;
console.log(alias.name);
});
如您所见,this.author
是在categoryCollection
posts.author
中找到的作者id。我试图在 usersWordpressCollection
中找到与作者匹配的 id,然后我想显示 id[= 作者的 name field
57=] 匹配。但我看不到让它工作,我该如何实现。
由于每个 post 只有一位作者,因此您无需遍历作者游标,您只需要一个。那么你在哪里:
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
改为使用:
{{#with author}}
<p>
{{name}}
</p>
{{/with}}
现在,进入 authors
模板的数据上下文将是一个 post
对象(因为您在 {{#each posts}}
中。因此您可以构建 author
助手如下:
Template.authors.helpers({
author(){
return usersWordpressCollection.findOne({ id: parseInt(this.author) });
}
});
既然您已经指出 this.author
是一个字符串,但您的用户集合的 id
是一个整数,那么您需要转换类型 - 您可以直接在查询中执行此操作。