5

I've been banging my head against the wall for a while now, and I assume I'm missing something simple here.

I'm running this on my Meteor server:

// --- 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}});
});

This finds all of the users who are on the same "team" by running a query on all user documents who have the same id in the profile._team property as the currently logged in user. You'll see the console.log(...); in the publish function (on the line before the return statement), and it correctly logs the documents I expect it to in my terminal.

Now I'm running this on my client:

// --- Data ---
Meteor.subscribe('team');
Team = new Meteor.Collection('team');

Template.team.team = function() {
    console.log(Team.findOne());
    return Team.find();
};

However, the console.log(Team.findOne()) always logs undefined, Team.find() always returns an empty array. What am I doing incorrectly that is stopping my documents from reaching the client?

UPDATE: Here's the template code.

<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" is never rendered inside the {{#each}} tag, but "TEAM TEMPLATE WORKS" renders as expected when it is placed before the {{#each}} tag.

4

2 に答える 2

3

問題は次のとおりです。

クライアントでは、コレクションを参照します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 メソッドは、カーソルで 1 回だけ呼び出すことができます。カーソル内のデータに複数回アクセスするには、巻き戻しを使用してカーソルをリセットします。

于 2013-05-25T23:11:25.107 に答える
2

meteor を使用すると、サブスクライブするのに非常に短い時間がかかりteamます。実行中、これTeam.findOne()は undefined を返しTeam.find()、空の配列を返します。

1 ~ 2 秒待つと、クライアントに表示されるデータが一致するはずです。

return Team.find()リアクティブなテンプレートヘルパーに配置しました。{{#each team}}したがって、HTML にヘルパーを使用する何かがある限り、データがクライアントに到着するとすぐに、UI は更新されたデータを表示する必要があります。

于 2013-05-26T15:09:12.023 に答える