客户端订阅不接收来自Meteor服务器发布的数据

win*_*toy 5 meteor

我现在已经把头撞到了墙上一段时间了,我想我在这里错过了一些简单的东西.

我在我的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}});
});
Run Code Online (Sandbox Code Playgroud)

通过在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();
};
Run Code Online (Sandbox Code Playgroud)

但是,console.log(Team.findOne())始终记录undefined,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>
Run Code Online (Sandbox Code Playgroud)

"TEAM EACH WORKS"永远不会在{{#each}}标签内部呈现,但"TEAM TEMPLATE WORKS"会在{{#each}}标签放置之前呈现预期效果.

Xya*_*and 3

问题是这样的:

在客户端上您引用集合team

Team = new Meteor.Collection('team');
Run Code Online (Sandbox Code Playgroud)

但是,在服务器发布函数中,您将光标返回到users

return Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}});
Run Code Online (Sandbox Code Playgroud)

team从未发表过任何文件。事实上,您甚至没有在服务器代码中使用Teamand 。Projects

旧答案:

尝试删除console.log(teamMates.fetch());或添加teamMates.rewind()

来自文档

forEach、map 或 fetch 方法只能在游标上调用一次。要多次访问游标中的数据,请使用倒带来重置游标。