Publish functions oddly not working at all


#1

I have the following code, i’m fairly certain it should work…
but it always only finds printinglist as an accessible collection.
it should actually be that i can access shops and allShops in chrome if i type those publication names in the console… I haven’t used meteor database functionality for a while but i don’t think anything changed here?

in collections.js in lib folder:

printinglist = new Mongo.Collection('printshops');
if (Meteor.isServer) {
    Meteor.startup(function() {
});


Meteor.publish('shops', function (city) {
   return printinglist.find({ cityid: city });
});    

Meteor.publish('allShops', function() {
   return printinglist.find();
});  

in my template:

Template.landing.onCreated(function () { 
  this.subscribe('allShops');
  this.subscribe('shops', Session.get('city'));
});

#2

I don’t get it: what would you want to see in the console?
Your code looks fine to me, except for the second subscribe that might better be inside an autorun.


#3

If I’d type in shops or allShops in the console it should show me both these publications/subsets of the collection, right? But this does not happen. Both shops and allShops are undefined


#4

shops and allShops aren’t JavaScript objects, so I don’t think this can work. A publication is just a function.


#5

Yea, my bad. Haven’t been using pub/sub for a while and I confused how it worked because of the reactivity I need. Could you give an example of how to do the reactivity for the publish function?

I subscribe like this now:

Template.landing.onCreated(function () { 
    this.autorun(function () {
        this.subscribe('shops', Session.get('city') );
    });
});

But I can’t get it to work to publish a new set based on data changes. I’ve tried it with tracker but I must be doing something wrong. I’m guesing it doesn’t recompute because there’s no reactive data source (the argument passed to the function is not reactive I’m guessing, so how to do it?)


#6

a little wrong this scope, it’s should be:


Template.landing.onCreated(function () { 
    var self = this;
    this.autorun(function () {
        self.subscribe('shops', Session.get('city') );
    });
});

// or ES6 syntax

Template.landing.onCreated(function () {
    this.autorun(() => {
        this.subscribe('shops', Session.get('city') );
    });
});


#7

The wrong scoping fixed it. It works reactively now. I have not defined anything with tracker in my publish. Thanks a lot :slight_smile: !