【问题标题】:Meteor Publish-Composite nesting issueMeteor Publish-复合嵌套问题
【发布时间】:2016-02-03 10:02:27
【问题描述】:

问题: 我有许多组,每个组都有属于不同组的成员。每个成员在每个组中都有一个头衔(角色)。 我正在尝试列出所有组并显示组中的每个成员及其标题。 我正在使用 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 
      },

收藏:

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)"
}

服务器/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);
  },
});

客户端/模板/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

【问题讨论】:

    标签: meteor meteor-collections


    【解决方案1】:

    从完全不同的角度来看,我实际上认为您可以使用 alanning:roles 来完成您正在寻找的内容。在这种情况下,您可以使用角色作为“标题”,并使用“组”来替换您的组。这是文档:

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

    【讨论】:

    • 感谢@StephenWoods - 这看起来可能有效。
    【解决方案2】:

    不确定,但我认为您的第二个 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 的孩子,而标题查询是所有标题。您的意思是在那里进行查询吗?

    【讨论】:

    • 感谢@MichaelFloyd,但数组方法触发了对象错误。
    【解决方案3】:

    我认为您的 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});
                  }
               }]
            }]
         }];
     });
    

    【讨论】:

      猜你喜欢
      • 2015-08-25
      • 2011-09-14
      • 2017-10-30
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2014-03-20
      相关资源
      最近更新 更多