Meteor Collection 模板中为空
Meteor Collection empty in template
我正处于 Meteor 项目的早期阶段,所有默认包都存在,包括自动发布。将 Collection Notes
添加到我的项目中工作正常并按预期显示。
当我添加第二个时Collection。 Tags
,复制相同的模式,它总是空的。
所以我需要一些注意事项。我读过例如David Weldons list of common mistakes 但没有发现任何线索。
有两个模板显示两个Collection,一个显示正常,一个显示不正常。
Collection的声明完全相同:
export const Tags = new Mongo.Collection('Tags');
export const Notes = new Mongo.Collection('Notes');
它们由类似的、简单的模板显示,这些模板在我的 html:
中引用
<body>
<div>
{{#each tags}}
{{> tag}}
{{/each}}
</div>
<div>
{{#each notes}}
{{> note}}
{{/each}}
</div>
</body>
模板助手也相同:
Template.body.helpers({
notes() {
return Notes.find({}, { sort: { createdAt: -1}});
},
tags() {
let t = Tags.find({});
return t;
},
});
如果我在 let t =...
之后中断,那么 t.fetch()
会给出一个空数组。 (meteor mongo: db.Tags.find()
是 不是 空...)
从 tags()
-helper 返回一个数组可以正确显示该数据,所以这显然是我在 Tags
-collection.
中遗漏的东西
这可能是时间问题?我认为助手是被动的,因此应该处理这些事情。或者我是否需要查看显式订阅,iron:router
和 waitOn
?
修复:
如@Jankapunkt 所述,我忘记将 collection 添加到服务器端启动。
为了成功地将您的 mongo 集合的数据自动发布到您的客户端,您需要将集合导入到服务器和客户端(最好在启动时)。
例子
imports/MyCollection.js
export const MyCollection = new Mongo.Collection('mycollection');
server/main.js
import { MyCollection } from '../imports/MyCollection';
client/main.js
import { MyCollection } from '../imports/MyCollection';
如果删除 autopublish
包,则需要在服务器端添加发布,在客户端添加订阅。
进一步阅读:
我正处于 Meteor 项目的早期阶段,所有默认包都存在,包括自动发布。将 Collection Notes
添加到我的项目中工作正常并按预期显示。
当我添加第二个时Collection。 Tags
,复制相同的模式,它总是空的。
所以我需要一些注意事项。我读过例如David Weldons list of common mistakes 但没有发现任何线索。
有两个模板显示两个Collection,一个显示正常,一个显示不正常。
Collection的声明完全相同:
export const Tags = new Mongo.Collection('Tags');
export const Notes = new Mongo.Collection('Notes');
它们由类似的、简单的模板显示,这些模板在我的 html:
中引用<body>
<div>
{{#each tags}}
{{> tag}}
{{/each}}
</div>
<div>
{{#each notes}}
{{> note}}
{{/each}}
</div>
</body>
模板助手也相同:
Template.body.helpers({
notes() {
return Notes.find({}, { sort: { createdAt: -1}});
},
tags() {
let t = Tags.find({});
return t;
},
});
如果我在 let t =...
之后中断,那么 t.fetch()
会给出一个空数组。 (meteor mongo: db.Tags.find()
是 不是 空...)
从 tags()
-helper 返回一个数组可以正确显示该数据,所以这显然是我在 Tags
-collection.
这可能是时间问题?我认为助手是被动的,因此应该处理这些事情。或者我是否需要查看显式订阅,iron:router
和 waitOn
?
修复:
如@Jankapunkt 所述,我忘记将 collection 添加到服务器端启动。
为了成功地将您的 mongo 集合的数据自动发布到您的客户端,您需要将集合导入到服务器和客户端(最好在启动时)。
例子
imports/MyCollection.js
export const MyCollection = new Mongo.Collection('mycollection');
server/main.js
import { MyCollection } from '../imports/MyCollection';
client/main.js
import { MyCollection } from '../imports/MyCollection';
如果删除 autopublish
包,则需要在服务器端添加发布,在客户端添加订阅。
进一步阅读: