简体   繁体   English

发布订阅似乎不起作用

[英]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 ! 希望我能清楚地描述我的问题!

Pub/Sub just fills your Client collection Channels . 发布/订阅仅填充您的客户收集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. 您可能有多个订阅,它们填充了Channels集合的不同文档,但是所有订阅最终都在客户端的那个集合中。

Then you have to adjust your query on client side to get the documents you need (eg Channels.find({'_id': {$nin: user.profile.channelIds}}); on Client as well). 然后,您必须在客户端上调整查询以获取所需的文档(例如,在客户端上也要使用Channels.find({'_id': {$nin: user.profile.channelIds}}); )。 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? 另请参阅如何控制两个订阅在单个模板中显示?

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 如果您订阅以获取Pages集合中定义为new Meteor.Collection(“ pages”)的文档,则无论您的发布渠道如何,在客户端上都将在定义为new的集合中找到该文档

> Meteor.Collection("pages")

. So remove all traces of MyPages and use Pages on the client as well. 因此,请删除MyPages的所有痕迹,并在客户端上也使用Pages。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM