John Dri
John Dri

Reputation: 29

Publish subscribe doesn't seem to work

I'm new to meteor and I saw that it's better to remove autopublish. So I try to publish and subscribe a collection to get two different values. In my meteor side I have :

Meteor.publish('channelUser',
    function(){
    var user = Meteor.users.findOne({
        '_id':this.userId
    });
        console.log(user);
       var test = Channels.find({
            '_id': {$in : user.profile.channelIds}
        });
        return test;
    }
);

Meteor.publish('channelToJoin',
function(){
    var user = Meteor.users.findOne({
        '_id':this.userId
    });
    console.log(user);

    var test = Channels.find({'_id': {$nin: user.profile.channelIds}});
    console.log(test);
    return test;
});

And in my client side in a first component I have :

this.channelSub = MeteorObservable.subscribe('channelUser').subscribe();
   this.channels = Channels.find({});

And on a second component :

 Meteor.subscribe("channelToJoin");
this.channels = Channels.find({}).zone();

But on my client side on both of the component, I have the same data. Is there some kind of conflict in the subscribe ?

I hope I was clear to describe my problem !

Upvotes: 0

Views: 82

Answers (2)

ghybs
ghybs

Reputation: 53185

Pub/Sub just fills your Client collection Channels. You can see it as a flow filling your local bucket. You may have several subscriptions filling different documents of Channels collection, but all end up in that single collection on the Client.

Then you have to adjust your query on client side to get the documents you need (e.g. Channels.find({'_id': {$nin: user.profile.channelIds}}); on Client as well). Of course you may have different queries in different templates, and different from the server publication as well.

See also How do I control two subscriptions to display within a single template?

Upvotes: 1

Muhammad Mohsin Muneer
Muhammad Mohsin Muneer

Reputation: 433

You cannot move a document between collections via a subscription. If you subscribe to get a document that's in Pages collection, defined as new Meteor.Collection("pages"), then no matter how your pub channels look like, on the client the document will be found in the collection defined as new

> Meteor.Collection("pages")

. So remove all traces of MyPages and use Pages on the client as well.

Upvotes: 0

Related Questions