流星:用户登录后,发布功能需要刷新页面 [英] Meteor: Publish function requires a page refresh after user logs in

查看:54
本文介绍了流星:用户登录后,发布功能需要刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流星应用程序,我在如下所示的铁路由器配置中一次调用了所有发布功能.我只想在用户登录后返回订阅,所以我检查Meteor.userId():

I have a meteor app and I'm calling all the publish functions at once in the iron router configuration like below. I only want to return the subscriptions once the user is logged in, so I check Meteor.userId():

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading',
  notFoundTemplate: '404',
  waitOn: function () {
    if (Meteor.userId()) {
      return [Meteor.subscribe('users'),
          Meteor.subscribe('company'),
          Meteor.subscribe('projects'),
          Meteor.subscribe('columns'),
          Meteor.subscribe('cards'),
          Meteor.subscribe('contents'),
          Meteor.subscribe('labels'),
          Meteor.subscribe('notifications')];
    }
  }
});

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

The publish functions have all the same structure, dependent on user.companyId, like this:

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保存到用户,但是当他们现在登录时,显示数据的唯一方法是刷新浏览器.我希望它是反应性的.

My problem is, when the user registers, the account is created and the companyId is saved to the user, but when they now login, the only way for the data to show up is to refresh the browser. I want it to be reactive.

推荐答案

来自流星指南:

在客户端上,如果反应功能中的任何内容发生变化,则整个 函数将重新运行,结果相当直观.

On the client, if anything in a reactive function changes, the whole function will re-run, and the results are fairly intuitive.

但是,在服务器上,反应性仅限于以下行为: 从发布函数返回的游标.您会看到任何 更改了与其查询匹配的数据,但他们的查询 永远不会改变.

On the server however, the reactivity is limited to the behavior of the cursors you return from your publish functions. You’ll see any changes to the data that matches their queries, but their queries will never change.

您确实可以按照建议使用 reywood:publish-composite ,但仅用于简单的情况我认为反应发布会更容易启动和运行.

You can indeed use reywood:publish-composite as suggested, but for your simple case I think reactive-publish would be much easier to get up and running.

安装软件包,然后将出版物包装在this.autorun中:

Install the package, and just wrap your publication in a 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();
    }
  });
});

这篇关于流星:用户登录后,发布功能需要刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆