How to Relationships one to many?


I’am try to create follower system llike twitter and I want to show posts only for fowlloing users
I’am dont sure with my publish code

I have 2 collection
1- Posts {
"_id": “3HrXtbHZ8WqHJJpyC”,
“post”: " some text",
“owner”: “Wdc84ED9zt4Ssxsh4”,
“username”: “MJK”,
“createdAt”: “2016-01-28T11:53:33.253Z”

2- Follwers {
"_id": "ShKWgeaP9BFPuQWwL",
  "follower": "MJK",
  "following": "michael"
**My code**

publish code

Meteor.publish('posts', function() {
  var user = Meteor.users.findOne({_id: this.userId}).username;
  var follower = Followers.find({followers: user}).following;
  var posts = Posts.find({username: {$in: follower}});
  return posts;



##client code

‘posts’: function(){
return Posts.find({}, {sort:{createdAt: -1}});


{{#each posts}}
<li class="collection-item avatar">
  <span class="title"><a href="/{{username}}">{{posts.username}}</a></span>



but no posts appears in browser
i think my publish code is worng can any one helpe me please

You can use publish:composite, cottz:publish-relation, peerlibrary:related or dburles:collection-helpers.

This packages are great for relation and joining collections.

1 Like

thank you I will try this
but isn’t there way to make this wlthout packages?

Everything beyond the basics require community packages in Meteor.

1 Like

thank you I’am traing those packages right now :yum:

IMHO, this should have been baked into the core.

1 Like