我已经把头撞在墙上一段时间了,我想我在这里遗漏了一些简单的东西。
我在我的 Meteor 服务器上运行它:
// --- Collections ---
Projects = new Meteor.Collection('projects');
Team = new Meteor.Collection('team');
// --- Only publish user data for users on my team ---
Meteor.publish('team', function() {
var team = Meteor.users.findOne({_id: this.userId}).profile._team;
console.log(Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}}).fetch());
return Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}});
});
profile._team
这通过对属性中与当前登录用户具有相同 id 的所有用户文档运行查询来查找位于同一“团队”中的所有用户。您将console.log(...);
在发布函数中看到 (在 return 语句之前的行),它正确地记录了我希望它在我的终端中的文档。
现在我在我的客户端上运行它:
// --- Data ---
Meteor.subscribe('team');
Team = new Meteor.Collection('team');
Template.team.team = function() {
console.log(Team.findOne());
return Team.find();
};
但是,console.log(Team.findOne())
总是记录未定义,Team.find()
总是返回一个空数组。我做错了什么导致我的文件无法到达客户手中?
更新: 这是模板代码。
<body>
{{> team}}
</body>
<template name="team">
<p>TEAM TEMPLATE WORKS</p>
{{#each team}}
<p>TEAM EACH WORKS</p>
<div class="teamMember">
{{profile.firstName}} {{profile.lastName}}
</div>
{{/each}}
</template>
“TEAM EACH WORKS”永远不会在{{#each}}
标签内呈现,但“TEAM TEMPLATE WORKS”放在{{#each}}
标签前时会按预期呈现。