5

我已经把头撞在墙上一段时间了,我想我在这里遗漏了一些简单的东西。

我在我的 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}}标签前时会按预期呈现。

4

2 回答 2

3

这是问题所在:

在客户端上,您指的是 collection team

Team = new Meteor.Collection('team');

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

return Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}});

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

老答案:

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

文档

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

于 2013-05-25T23:11:25.107 回答
2

使用流星将需要很短的时间来订阅team。当它这样做时,这Team.findOne()将返回 undefined 并Team.find()给出一个空数组。

如果您等待一两秒钟,客户端上显示的数据应该匹配。

您确实将您的模板放在return Team.find()了一个响应式的模板助手中。因此,一旦数据到达客户端,只要您的 HTML 中有使用{{#each team}}帮助器的内容,UI 就应该显示更新的数据

于 2013-05-26T15:09:12.023 回答