我是Meteor的初学者,我无法完成迭代集合并将每个项目的单个属性打印到模板的简单任务。我打开了autopublish,所以我还没有写过pub / sub函数。到目前为止我所做的所有其他组件都有效,所以我很确定问题出现在下面的代码中。
在HTML中(注释评论)
<template name="tags">
<div class="tags">
{{#each printTags }}
{{name}} <br>
{{/each }}
<!-- why is nothing showing up here? -->
</div>
<template>
在JS文件中
if (Meteor.isClient) {
Tags = new Mongo.Collection("tags")
tags = Tags.find({}).fetch();
Template.tags.helpers({
printTags: tags
});
}
在客户端上,在Dev Tools中
Tags.find().fetch().forEach(function(tag){console.log(tag["name"])})
1
2
3
4
5
答案 0 :(得分:0)
代码的问题在于,当集合尚未填充数据时,您的printTags
帮助程序被分配给变量(tags
),其中包含获取集合文档的结果。
您必须使用函数,以便在数据可用时对表达式进行反应性重新评估:
Template.tags.helpers({
printTags: function(){
return Tags.find();
}
});