发布订阅似乎不起作用 [英] 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({});
在第二个组件上:
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仅填充您的客户端集合 Channels
.您可以将其视为填充本地存储桶的流.您可能有多个订阅填充了 Channels
集合的不同文档,但是所有订阅最终都存储在Client上的单个集合中.
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.
然后,您必须在客户端上调整查询以获取所需的文档(例如, Channels.find({'_ id':{$ nin:user.profile.channelIds}});
也可以在客户端上).当然,您在不同的模板中可能会有不同的查询,并且与服务器发布也有所不同.
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.
这篇关于发布订阅似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!