【问题标题】:Meteor Publications/Subscriptions not working流星出版物/订阅不起作用
【发布时间】:2016-08-26 10:43:20
【问题描述】:

我是个菜鸟,在让我的出版物正常工作时遇到了一些麻烦。在我的数据中,我有许多患者,并且想显示单个患者的数据。这就是我组织出版物的方式:

Meteor.publish('patients.single', function (patientId) {
  check(patientId, String);
  return Patients.find({_id: patientId});
});

这就是我订阅的方式:

Router.route('/patients/:_id', {
  layoutTemplate: 'ApplicationLayout',
  yieldRegions: {
    'single_patient': {to: 'content'}
  },
  subscriptions: function () {
    return Meteor.subscribe('patients.single', this.params._id);
  }
});

我也尝试通过实际模板订阅,但无济于事:

Template.patient_details.onCreated(function () {
  this.subscribe('patients.single', Session.get("currentPatient"));
});

从理论上讲,出版物似乎很容易,但我似乎无法正确理解它们。我在这里做错了什么?

【问题讨论】:

    标签: meteor meteor-publications


    【解决方案1】:

    订阅将数据从服务器获取到 mini mongo 需要时间,因此您必须等待订阅准备好,然后才能使用它将为您获取的数据。

    如果您使用 Iron Router,请尝试使用 waitOn 而不是 subscribe,这将强制路由器等待订阅准备好并在获取订阅数据时呈现加载模板。

    Router.route('/patients/:_id', {
      layoutTemplate: 'ApplicationLayout',
      yieldRegions: {
        'single_patient': {to: 'content'}
      },
      waitOn: function () {
        return Meteor.subscribe('patients.single', this.params._id);
      }
      data: function () {
        return Patients.findOne({_id: this.params._id});
      },
    });
    

    您也可以使用 data 属性,这样您就可以在模板 instance.data 中获得数据。

    【讨论】:

      【解决方案2】:

      试试这个:

      服务器端Js

      Meteor.publish('patients.single', function (patientId) {
        check(patientId, String);
        return Patients.find({_id: patientId});
      });
      

      路由器 JS 文件

      Router.route('/patients/:_id', {
        layoutTemplate: 'ApplicationLayout',
        yieldRegions: {
          'single_patient': {to: 'content'}
        },
        waitOn: function () {
          return Meteor.subscribe('patients.single', this.params._id);
        }
      });
      

      在客户端 JS 文件中

      Template.patient_details.helpers({
        getData : function(){
              return Collection.find().getch();
      
      });
      

      不要忘记在模板html文件中调用{{getData}}。

      【讨论】:

      • 谢谢。我试过这个,但不知何故它不起作用......我不知道为什么。可能是因为我有同一系列的其他出版物吗?
      • 您可以编辑问题并添加模板文件吗?这不是因为多个出版物。您可以从同一个集合中发布多个出版物。
      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 2018-12-18
      • 2018-12-26
      • 1970-01-01
      • 2015-09-02
      • 2015-05-02
      相关资源
      最近更新 更多