【问题标题】:Why isn't Meteor.subscribe receiving data from a publication为什么 Meteor.subscribe 不从出版物中接收数据
【发布时间】:2017-03-03 17:03:32
【问题描述】:

我有这个出版物

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()
        }
    }
});

我在这里做错了什么?

【问题讨论】:

    标签: meteor publish-subscribe


    【解决方案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方法在订阅准备好之前不会被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      相关资源
      最近更新 更多