javascript - 使用发布复合进行共享连接: pull out user's posts + comments

标签 javascript meteor publish-subscribe

我能够显示特定用户的帖子列表,但无法显示包含特定用户评论的帖子。

官方链接中的示例使用此层次结构 here

对于我的问题,层次结构是:

all posts -> 
(children)
-> find all post by user
-> find all comment by user
   (children)
   -> find post that has id that matches comments

到目前为止我所拥有的..

发布复合线

Meteor.publishComposite('user', function(_id) {  
    return { 
      find: function() { 
        return Meteor.users.find({_id: _id}, { fields: { profile: 1, username: 1} });
      },
      children: [
        {
          find: function(user) { return Posts.find({ userId: user._id }); }
        },
        {
          find: function(user) { return Comments.find({ _id: user._id }); } 
              children: [
                {
                  find: function(comment) { return Posts.find({ _id: comment.postId }); }
                } 
              ] 
        }
      ]
    };
});

在模板中

Template.usersShow.helpers({    
  user: function () {
    return Meteor.users.findOne({ _id: Router.current().params._id });
  }
});

在路由器中

Router.route('/users/:_id', { name: 'users.show',
  waitOn:function(){
    // console.log(this.params._id);
    return Meteor.subscribe('user', this.params._id);
  }
});

html

  {{#with user}}
    <div class>{{ profile.name }}</div>    <!-- name appears --> 
    {{#ionList}}
      {{#each post}}
        {{> postItem}}
      {{/each}}
    {{/ionList}}
  {{/with}}

最佳答案

我第一眼看到的是:

  • 您无需发布 pullData无论如何,user出版物可以完成您在 1) 中提到的工作
  • 在您的代码中尝试按照以下模式查找 postscomments属于user :

children: [ { find: function(user) { return Posts.find({ authorId: user._id }); } }, { find: function(user) { return Comments.find({ authorId: user._id }); } } ] 只需替换 authorIdPost 中使用适当的字段名称和Comment方案。

我找到了publishComposite在 Meteor 中发布连接的优秀解决方案。

Join in Meteor with publishComposite

希望这有帮助,

亚历克斯

附注我已重新阅读您编辑过的问题,并建议您:

  • 将您的应用程序拆分为 View (阅读模板),例如AllPosts , MyPosts TopTenPosts , PostsByUser , PostWithComments , MyCommentedPosts等等
  • 定义每个 View 必须显示的内容,并将内容转换为订阅
  • 为每个订阅定义发布(您可能想以某种方式对其进行 stub )
  • 确保您的观点符合预期
  • 重构出版物,以便它们仅从 mongo 中提取特定 View 所需的数据

TDD 来拯救。

关于javascript - 使用发布复合进行共享连接: pull out user's posts + comments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699768/

相关文章:

javascript - NodeJS GZip 压缩 - 有问题吗?

javascript - 遍历树并将所有属性推送到数组中

javascript - 在javascript中用另一个数组过滤多维数组

meteor 循序渐进我的第一个应用指南?

java - 谷歌发布-订阅 setMaxMessages

ruby - 我如何找出谁连接到 ActionCable?

javascript - 需要帮助添加一个额外的回调函数

android - meteor android sdk,是否可以安装在ubuntu vm(virtualbox)或windows上

meteor - Meteor 中的 Markdown 无法正确呈现

redis - psubscribe with '*' 模式是个坏主意?