Meteor:发布功能要求用户登录后刷新页面

Meteor:发布功能要求用户登录后刷新页面,meteor,iron-router,Meteor,Iron Router,我有一个meteor应用程序,我在iron路由器配置中一次调用所有发布函数,如下所示。我只想在用户登录后返回订阅,所以我检查Meteor.userId() 发布函数具有相同的结构,具体取决于user.companyId,如下所示: Meteor.publish('cards', function () { if (this.userId) { const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exi

我有一个meteor应用程序,我在iron路由器配置中一次调用所有发布函数,如下所示。我只想在用户登录后返回订阅,所以我检查Meteor.userId()

发布函数具有相同的结构,具体取决于user.companyId,如下所示:

Meteor.publish('cards', function () {
  if (this.userId) {
    const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exists: true } });

    if (user) {
        return Cards.find({ companyId: user.companyId });
    }
  } else {
    this.ready();
  }
});
我的问题是,当用户注册时,会创建帐户并将companyId保存给用户,但当用户现在登录时,显示数据的唯一方法是刷新浏览器。我希望它是反应性的。

来自:

在客户机上,如果反应式函数中有任何变化,则整个 函数将重新运行,结果相当直观

然而,在服务器上,反应性仅限于 从发布函数返回的游标。你会看到任何 对与其查询匹配的数据所做的更改,但其查询 永远不会改变

您确实可以按照建议使用,但对于您的简单案例,我认为将更容易启动和运行

安装软件包,只需将出版物包装在一个
this.autorun

Meteor.publish('cards', function () {
  this.autorun( function() {
    if (this.userId) {
      const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exists: true } });

      if (user) {
          return Cards.find({ companyId: user.companyId });
      }
    } else {
      this.ready();
    }
  });
});

您可以使用publish composite来实现这一点。另外,我认为您希望在
else
语句中
return
Meteor.publish('cards', function () {
  this.autorun( function() {
    if (this.userId) {
      const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exists: true } });

      if (user) {
          return Cards.find({ companyId: user.companyId });
      }
    } else {
      this.ready();
    }
  });
});