【问题标题】:Publish subscribe doesn't seem to work发布订阅似乎不起作用
【发布时间】:2017-03-31 21:02:30
【问题描述】:

我是流星新手,我发现最好删除自动发布。 所以我尝试发布和订阅一个集合以获得两个不同的值。 在我的流星身边,我有:

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

在我的客户端中,我有第一个组件:

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

在第二个组件上:

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

但在两个组件的客户端,我拥有相同的数据。 订阅中是否存在某种冲突?

我希望我能清楚地描述我的问题!

【问题讨论】:

    标签: javascript angular meteor


    【解决方案1】:

    Pub/Sub 只会填充您的客户集合 Channels。 您可以将其视为填充本地存储桶的流。您可能有多个订阅填写 Channels 集合的不同文档,但最终都在客户端上的单个集合中。

    然后你必须在客户端调整你的查询以获得你需要的文档(例如Channels.find({'_id': {$nin: user.profile.channelIds}}); 在客户端也是如此)。当然,你可能在不同的模板中有不同的查询,也可能与服务器发布不同。

    另见How do I control two subscriptions to display within a single template?

    【讨论】:

    • 我想我明白了!谢谢你
    【解决方案2】:

    您不能通过订阅在集合之间移动文档。如果您订阅获取 Pages 集合中的文档,定义为 new Meteor.Collection("pages"),那么无论您的 pub 频道看起来如何,在客户端上,该文档都将在定义为 new 的集合中找到

    > Meteor.Collection("pages")
    

    。因此,删除 MyPages 的所有痕迹并在客户端上使用 Pages。

    【讨论】:

    • 所以基本上,我必须替换: export const Channels = new MongoObservable.Collection('channels');在 Meteor.Collection("collection") 我的两个/集合中?我很确定我不明白你的意思抱歉:(
    • 没问题,亲爱的,没问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多