为什么没有Meteor订阅从出版物接收数据

时间:2017-03-03 17:03:33

标签: meteor publish-subscribe

我有这个出版物

Meteor.publish('user', function(id){
    if(id == this.userId) {
        return Meteor.users.find({_id: id}, {fields: {dob: 1, name: 1, ability: 1, location: 1, clubs: 1, coach: 1, friends: 1}});
    } else {
        var user = Meteor.users.findOne({_id: id});
        var fields = {};
        if(user.dob.allowed)
            fields.dob = 1;
        if(user.ability.allowed)
            fields.ability = 1;
        if(user.coach.allowed)
            fields.coach = 1;
        if(user.location.allowed)
            fields.location = 1;
        if(user.clubs.allowed)
            fields.clubs = 1;
        if(user.friends.allowed)
            fields.friends = 1;

        fields.name = 1;

        return Meteor.users.find({_id: id}, {fields: fields});
    }
});

然而,当我在客户端上订阅这个并尝试在我刚刚订阅的用户上找到一个时,返回是未定义的。

Router.route('/user/:_id/profile', {
    template: "home",
    yieldRegions: {
        "profile": {to: "option"}
    },
    data: function() {
        Meteor.subscribe('user', this.params._id);
        return Meteor.Users.findOne({_id: this.params._id});
    },
    onBeforeAction: function() {
        if(Meteor.userId() == null) {
            Router.go('/');
        } else {
            this.next()
        }
    }
});

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您按照approach in the Iron Router guide进行操作,则可能需要按如下方式设置路线:

Router.route('/user/:_id/profile', {
  template: "home",
  yieldRegions: {
    "profile": {to: "option"}
  },
  // make the route wait on the subscription to be ready
  waitOn: function() {
     Meteor.subscribe('user', this.params._id);
  },
  data: function() {
    return Meteor.Users.findOne({_id: this.params._id});
  },
  onBeforeAction: function() {
    if(Meteor.userId() == null) {
      Router.go('/');
    } else {
      this.next()
    }
  }
});

这样,在订阅准备就绪之前,不会调用您的路由data方法。