Meteor Publish-Composite 嵌套问题

Meteor Publish-Composite nesting issue

问题: 我有许多组,每个组都有属于不同组的成员。每个成员在每个组中都有一个头衔(角色)。 我正在尝试列出所有组并显示组中的每个成员及其头衔。 我正在使用 reywood:publish-composite,一切正常,只是我无法显示每个成员的标题。 我认为问题出在 Template.groupMembers.helpers 文件

 title: function() {
        console.log(this.roleId); // this shows up in the console for each member
        return Titles.findOne({titleId: this.roleId}); // but this doesn’t work 
      },

Collections:

groups {
    "_id" : "xFSzAHBEps2dSKcWM",
    "name" : "Generic Group",
    "logo" : "generic-logo-hi.png"
}


members {
    "_id" : "vyDtiaKKukZYQdFvs",
    "groupId" : "xFSzAHBEps2dSKcWM",
    "memberId" : "hRx8GBTyB5X8iQQ52",
    "roleId" : "1"
}


Meteor.users {
    "_id" : "hRx8GBTyB5X8iQQ52",
    "profile" : {
        "name" : "Bob Lorros"
    },
 }


titles {
    "_id" : "bYsKpsyYtyKR8NYpm",
    "titleId" : 1,
    "title" : "Staff (non-voting)"
}

server/publications/publications.js

Meteor.publishComposite('groupMembers', {
  find: function() {
      return Groups.find({}, {
        sort: {name: 1}
      });
    },
    children: [
        {
            find: function() {
                return Titles.find();
            },
            find: function(group) {
              return Members.find({groupId: group._id});
            },
            children: [
              {
                find: function(member) {
                    return Meteor.users.find({_id: member.memberId});
                }
              },
            ]
        },
    ]
});

client/templates/test/test.js

Template.groupMembers.helpers({
  groupMembers: function() {
    return Groups.find({}, {
      sort: {name: 1}
    });
  },
  members: function() {
    return Members.find({groupId: this._id});
  },
  title: function() {
    console.log(this.roleId); // this shows up in the console for each member
    return Titles.findOne({titleId: this.roleId}); // but this doesn’t work 
  },
  memberName: function() {
    return Meteor.users.findOne(this.memberId);
  },
});

client/templates/test/test.html

<template name="groupMembers">
  <h4>Group - Members</h4>
  {{#each groupMembers}}
    <b>{{name}}</b><br>
    {{#each members}}
       &nbsp;{{memberName.profile.name}}
        - title = {{title.title}}
        <br>
    {{/each}}
    <br>
  {{/each}}
</template>

输出: This is the ouput

从一个完全不同的角度来看,我实际上认为您可以使用 alanning:roles 来完成您正在寻找的东西。在这种情况下,您可以使用角色 'title' 和 'group' 来替换您的组。这是文档:

https://github.com/alanning/meteor-roles

不确定,但我认为您的第二个 find 可能会覆盖您的第一个。而不是:

find: function() {
  return Titles.find();
},
find: function(group) {
  return Members.find({groupId: group._id});
},

尝试返回游标数组。

find: function() {
  return [
    Titles.find(),
    Members.find({groupId: group._id})
  ];
},

但是我不明白为什么当标题查询是所有标题时,TitlesGroupMembers 的 child。您是要在那里查询吗?

我认为您的 publishComposite 导致了问题,children 数组中的每个对象应该只有一个 find 和零个或多个 children。此外,您发布的第二个参数必须是一个函数,而不是 JSON 对象。试试这个,

 Meteor.publishComposite('groupMembers', function () {
     return {
        find: function() {
            return Groups.find({}, {
               sort: {name: 1}
            });
        },
        children: [{
           find: function() {
              return Titles.find();
           }
        },
        {
           find: function(group) {
              return Members.find({groupId: group._id});
           },
           children: [{
              find: function(member) {
                 return Meteor.users.find({_id: member.memberId});
              }
           }]
        }]
     };
 });

您还可以通过将 Titles.find 移动到根级别

来提高性能
    Meteor.publishComposite('groupMembers', function () {
     return [{
        find: function() {
           return Titles.find();
        }
     }, {
        find: function() {
            return Groups.find({}, {
               sort: {name: 1}
            });
        },
        children: [{
           find: function(group) {
              return Members.find({groupId: group._id});
           },
           children: [{
              find: function(member) {
                 return Meteor.users.find({_id: member.memberId});
              }
           }]
        }]
     }];
 });