I am quite a newbie at Meteor. I naively thought that a "publish" on the server would send across to the client a set/subset of a collection into a synchronised minimongo collection, but more importantly that the subscribed client would only be allowed to deal with that published set of data (no more, no less). However when subscribing inside a template helper, it looks like:
1) you cannot use the name of the 'publish' (what 's its use then?)
2) you have to query the collection and do the filtering again (why then bothering filtering in the 'publish'?)
Here is the code:
meteor:PRIMARY> db.products.insertMany([
... { "name" : "product_1", "color" : "black", "taste" : "salty" } ,
... { "name" : "product_2", "color" : "black", "taste" : "sweet" } ,
... { "name" : "product_3", "color" : "white", "taste" : "salty" } ,
... { "name" : "product_4", "color" : "white", "taste" : "sweet" } ])
template:
<template name="products">
<h2> All of them:</h2>
{{#each allOfThem}}
<li>
{{name}} {{color}} {{taste}}
</li>
{{/each}}
<h2> salty ones:</h2>
{{#each saltyOnes}}
<li>
{{name}} {{color}} {{taste}}
</li>
{{/each}}
</template>
in main.js - server:
Meteor.startup(() => {
Meteor.publish('saltyProducts', function(){
return Products.find( { taste : "salty"} );
});
});
in main.js - client:
Template.products.onCreated( function() {
Meteor.subscribe("saltyProducts");
});
Template.products.helpers({
allOfThem: function(){
return Products.find( {});
},
saltyOnes: function(){
return Products.find( { taste : "salty"} );
}
});
and the result:
All of them, instead of my subscription..
product_1 black salty
product_2 black sweet
product_3 white salty
product_4 white sweet
salty ones, but I had to filter in the helper..
product_1 black salty
product_3 white salty
Of course I have tried to do: return saltyProducts.find( {} ); but that does not work. Again, what's the point having defined the "saltyProducts" publication?
saltyProductsis the name of your publication. Like an identifier for a dataset you will return from your.publish()method. It is for your information, like which dataset you want to receive when you call.subscribe()- Artūrs Lataks