Search code examples
javascriptnode.jsmongodbmeteorflow-router

meteor: conditional subscription in template level


i reuse the same template in other route with different data argument but using the same publication...

if i do normal pub/sub, the data being published as expected. but when i do conditional pub/sub like below, i fail to subscribe the data. console log return empty array,,,

server/publication.js

Meteor.publish('ACStats', function(cId, uId) {
    var selectors = {cId:cId, uId:uId};
    var options = {
        fields: {qId:0}
    };
    return ACStats.find(selectors,options);
});

client/onCreated

Template.channelList.onCreated(function() {
  this.disable = new ReactiveVar('');
  if (FlowRouter.getRouteName() === 'profile') {
    var self = this;
    self.autorun(function() {
      var penName = FlowRouter.getParam('penName');
      var u = Meteor.users.findOne({slugName:penName});
      if (u) {var uId = u._id;}
      Meteor.subscribe('ACStats', null, uId);
    });
  } else{
    var self = this;
    self.autorun(function() {
      var channelName = FlowRouter.getParam('channel');
      var c = Channels.findOne({title:channelName});
      if (c) {var cId = c._id;}
      Meteor.subscribe('ACStats', cId, null);
    });
  }
});

console

ACStats.find().fetch() //return empty array

anyone have figured out my mistake ..??

thank You so much....


Solution

  • You can make two publications:

    Meteor.publish ('ACStatsChannels', cId, function() {
    });
    Meteor.publish ('ACStatsUsers', uId, function() {
    })
    

    And then subscribe like this:

    Template.channelList.onCreated(function() {
       this.disable = new ReactiveVar('');
       var self = this;
       self.autorun(function() {
          if (FlowRouter.getRouteName() === 'profile') {
              var penName = FlowRouter.getParam('penName');
              self.subscribe('ACStatsUsers', penName);
          } else {
              var channelName = FlowRouter.getParam('channel');
              self.subscribe('ACStatsChannels', channelName);
        }
      });
    });