首页 > 解决方案 > 如何让 Meteor 的子项发布Composite

问题描述

我坚持使用 Meteor 的 publishComposite。我不明白在 publishComposite 中定义子集合的正确方法是什么。

假设我有这样的接口

interface IInterview {
  _id: string,
  candidateId: string,
  comments: IInterviewComments[]
}

interface IInterviewComment {
  userId: string,
  text: string
}

interface IUser {
  _id: string,
  name: string
}

因此,根据一些采访的数据,我还需要评论作者的姓名

我写了这样的东西

publishComposite('interviews.item', function(interviewId) {
  return {
    find() {
      return InterviewCollection.find({_id: interviewId});
    },
    children: [
      find(interview) {
        const authorsIds = interview.comments.map((comment) => comment.userId);

        return UsersCollection.find({_id: {$in: authorsIds}});   
      }
    ]
  }
});

那么,我应该如何在客户端获取评论作者的姓名?我可以简单地执行UsersCollection.findOne({_id: commentUserId})还是应该使用一些收集助手?

标签: javascriptmeteor

解决方案


publishComposite 仅将所有相关数据发布到客户端对应的集合下。然后,您可以像往常一样自由地访问它,或者按照您认为合适的方式抽象它。您可以使用不同的方法进行抽象。您可能会查看的特定包是jagi:astronomydburles:collection-helperssocialize:base-model.

如果您想要将数据组合在一起的东西,以便在您接受采访时,采访的评论位于评论属性中,并且评论的作者在每条评论中都指定,那么我建议您查看cultofcoders:grapher


推荐阅读