服务器上的 Meteor.publish 不会在客户端上显示新文档

Meteor.publish on server doesn't show new documents on client

本文关键字:新文档 显示 文档 客户端 Meteor publish 服务器      更新时间:2023-09-26

问题是服务器上的下一个代码:

Meteor.publish(null , function() {
    let events = [];
    Groups.find({participants: this.userId}).forEach(function(item) {
        events.push(item.latestEvent);
    });
    return Events.find({_id: {$in: events}});
});

不提供在客户端> Events.find().fetch()上查看新文档的可能性无需重新加载页面。

这两个集合都位于lib文件夹中:

Groups = new Mongo.Collection('groups');
Events = new Mongo.Collection('events');

我很确定问题出在反应式数据源中,但仍然无法解决它。

谢谢你的帮助!

是的,你是对的:只有事件集合是反应式的。有一个简单的方法可以通过使用发布复合包来解决它:

Meteor.publishComposite(null, {
    find(){
      return Groups.find({participants: this.userId});
    },
    children: [{
      find(group){
         return Events.find({_id: {$in: group.latestEvent}});
      }
    }]
});

但此解决方案有一个缺点:组文档也会发布。因此,您可能应该从中排除某些字段。