在Meteor 1.0.2中的路由上设置数据时,铁路由器waitOn不工作

时间:2014-12-22 18:15:51

标签: meteor iron-router

我正在定义一条显示患者预约的路线。我希望模板能够显示患者信息和预约信息。

我发布了这个:

Meteor.publish('userAppointment', function(appointmentId){
  check(appointmentId, String);
  var userId = Appointments.findOne(appointmentId).patientId;
  return [
    Appointments.find({_id: appointmentId}),
    Meteor.users.find({_id: userId}, {fields: {profile: true, emails: true}})
  ];
});

不幸的是,在尝试设置数据上下文之前,Iron Router似乎没有成功等待数据订阅完成。

注意调试器的放置位置:

Router.route('/admin/appointment/:id', {
  name: 'AppointmentShow',
  waitOn: function(){
    return [
      Meteor.subscribe("userAppointment", this.params.id)
    ]
  },
  data: function(){
    var appointmentId = this.params.id;
    debugger

    var patientId = Appointments.findOne(appointmentId).patientId;

    return {
      appointment: Appointments.findOne(appointmentId),
      patient: Meteor.users.findOne(patientId)
    }
  }
});

当调试器停止代码时,当我在控制台中执行Meteor.users.find().fetch()Appointments.find().fetch()时,只有当前登录的用户(我)可用,并且没有可用的约会。

我希望看到两个用户(我和病人)和一个约会可用,因为这是waitOn完成订阅后应该可用的数据。

我在这里错过了什么吗?

编辑-----对我来说仍然没有意义------

当我改变路线时:

Router.route('/admin/appointment/:id', {
  name: 'AppointmentShow',
  waitOn: function(){
    return [
      Meteor.subscribe("userAppointment", this.params.id)
    ]
  },
  data: function(){
    var appointmentId = this.params.id;
    return {
      appointment: Appointments.findOne(appointmentId),
      // patient: Meteor.users.findOne(Appointments.findOne(appointmentId).patientId)
    }
  }
});

Appointments.findOne(appointmentId)返回一个对象:

{
  _id: "23efref34qr2",
  reason: "coughing",
  patientId: "785g45g4f"
}

当我的数据函数只返回

appointment: Appointments.findOne(appointmentId)

它有效。但如果我有它也会返回

patient: Meteor.users.findOne(Appointments.findOne(appointmentId).patientId)

我收到错误消息(无法读取未定义的属性'patientId'。)嗯?它只是在上面的行中定义了!

2 个答案:

答案 0 :(得分:3)

为了澄清,我认为你应该允许你的数据函数运行(并在填充集合时重新运行),但是要小心确保你的函数在数据可用之前运行时不会抛出错误。这是一般的流星图案。

data: function(){ 
  var appointmentId = this.params.id,
        appointment = Appointments.findOne(appointmentId);
     return { appointment: appointment,
    patient: Meteor.users.findOne(appointment ? appointment.patientId : null) } 
  }

抱歉格式化,我是用一部糟糕的手机做的...

答案 1 :(得分:0)

问题似乎是它在运行waitOn之前运行数据函数(),因此取决于时序。我看到了同样的问题,还必须检查数据是否确实存在。