在下面的基本示例中,为什么渲染函数中返回的集合为空?
Autopublish已启用。页面加载后,在javascript控制台中调用命令
以下是代码:
Autopublish已启用。页面加载后,在javascript控制台中调用命令
Coll.find().fetch()
会返回正确的条目集。以下是代码:
t.js
Coll = new Meteor.Collection("coll");
if (Meteor.isClient) {
Template.tpl.rendered = function(){
console.log(Coll.find().fetch()); // <-- This line prints empty array
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Coll.find().count() === 0) {
var f = ["foo","bar"];
for (var i = 0; i < f.length; i++)
Coll.insert({f: f[i]});
}
});
}
以及 t.html
文件
<head>
<title>test</title>
</head>
<body>
{{> tpl}}
</body>
<template name="tpl">
Test tpl
</template>
Template.rendered
被触发,并不意味着你的集合已经加载完成。请查看此线程。 - sohel khalifa