Meteor React nested subscriptions


#1

I would like to know, what is the best practice for nested subscriptions when using Meteor and React through react-meteor-data. I would like to subscribe to data based on return of my first subscription i.e.,

export default createContainer(({params}) => {

const handleCountry = Meteor.subscribe('countries.name', name);
const loadingCountry = !handleCountry.ready();
const country = Countries.findOne({name: name});
const handlePresident = Meteor.subscribe('profile.id', country.president);
const president = Meteor.users.findOne({_id: country.president});
return {
    loadingCountry,
    country,
    president
}

}, CountryUI)


#2

You can move the “join” to publication-level with this package:
http://fastosphere.meteorapp.com/reywood%3Apublish-composite?q=publish

Then you’ll only need one sub which will return items to multiple collection.


#3

I believe that the join publication are supported by default in Meteor e.g., https://guide.meteor.com/data-loading.html#publishing-relations . But thank you anyway.

It seems like I have found what I was after - https://guide.meteor.com/data-loading.html#complex-auth


#4

They work, but as you can read in the guide, it will work differently as you might expect. In your example if the president changes in the meantime, the new one won’t be sent to the client.
They recommend publish-composite for this as well.


#5

Yea, this is something I bumped in as well. I ended up compiling following code to achieve the expected result:

export default createContainer(({params}) => {

const handleCountry = Meteor.subscribe('countries.name', name);
const loadingCountry = !handleCountry.ready();
const country = Countries.findOne({name: name});

let handlePresident, loadingPresident = false;
let president = {};

if(!loadingCountry){
    const handlePresident = Meteor.subscribe('profile.id', country.president);
    loadingPresident = !handlePresident.ready();
    president = Meteor.users.findOne({_id: country.president});
}

return {
    loadingData: loadingCountry || loadingPresident,
    country,
    president
}
}, CountryUI)