Meteor Publish-Composite 嵌套问题

标签 meteor meteor-collections

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

客户端/模板/测试/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.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 来完成您正在寻找的任务。在这种情况下,您可以使用角色作为“标题”,并使用“组”来替换您的组。这是文档:

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

关于Meteor Publish-Composite 嵌套问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165323/

相关文章:

javascript - 如何在空格键 #each 标签中显示来自两个不同集合文档的数据?

javascript - 实时过滤json

email - 在 Meteor 中验证电子邮件的正确方法

javascript - Meteor-Simple-Schema 中 Date.now() 的架构字段类型

javascript - Meteor find 使用变量返回 null

javascript - 如何移动 meteor 集合中的项目?

javascript - 我如何使用 meteor subscriptionsReady() 来确保数据在渲染模板之前就绪?

javascript - 将外部 react 性 API 数据插入到 Meteor 集合中

node.js - 在 Elastic Beanstalk 中更新 nodejs

mongodb - 在浏览器 Mongo DB 侧边栏中