0

Im able to show list of posts of a specific user but not able to show posts that contains comments by a specific user.

The example in the official links uses this hierarchy here

For my problem, the hierarchy is:

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

What I have so far..

Publish Composite lines

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 }); }
                } 
              ] 
        }
      ]
    };
});

In the template

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

In the router

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}}
Community
  • 1
  • 1
Thinkerer
  • 1,606
  • 6
  • 23
  • 43

1 Answers1

1

what i see at first glimpse is:

  • You don't need to publish pullData at all, user publication can do the job you mention in 1)
  • in your code try following pattern to find posts and comments belonging to user:

children: [ { find: function(user) { return Posts.find({ authorId: user._id }); } }, { find: function(user) { return Comments.find({ authorId: user._id }); } } ] Just replace authorId with appropriate field name in your Post and Comment schemes.

I found publishComposite excellent solution for publishing joins in Meteor.

Join in Meteor with publishComposite

Hope this helps,

Alex

P.S. I have re-read your edited question and would like to suggest you to:

  • split your app in views (read Templates), e.g. AllPosts, MyPosts TopTenPosts, PostsByUser, PostWithComments, MyCommentedPosts etc.
  • define what every view has to display, and convert what into subscription
  • define publication (you may want to stub it somehow) for every subscription
  • ensure your views are matching expectations
  • refactor publications so they pull only necessary data from mongo for particular view

TDD to rescue.

Community
  • 1
  • 1
joystick
  • 150
  • 1
  • 9
  • Hi, good to know its on track. How do I then reflect the posts in the html? So for example to show all the posts written by the user. My html doesnt show anything and no error message – Thinkerer Jul 29 '15 at 16:34
  • in browser console try Posts.find().fetch() and Comments.find().fetch() to check if published data arrived to browser and available for templates. Then make template helpers for 'posts` and 'comments' returning Posts.find() and Comments.find() accordingly. And last, take {{> postItem}} list out of {{#with user}} - you don't have posts and comments under user object. – joystick Jul 30 '15 at 14:09
  • i have update it for the Ids. Not too good with the naming conventions. I am looking to pull the posts that has the comments, and not the comments themselves. Is it harder and how do I do iit? I have an additional line under `Comments.find` but nothing shows up and console logs empty `[ ]`. – Thinkerer Aug 01 '15 at 10:14
  • 1
    In order to show **posts commented by user** you may want to change your query so `posts` will be children of `comments` and userId will be a `filter` parameter to find `comments`. Please check link above for example. – joystick Aug 04 '15 at 15:56