iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 413 forks source link

the data function for a route runs before waitOn is tested #1583

Closed dpatte closed 7 years ago

dpatte commented 7 years ago

this can cause incorrect data to be shown once the subscription is ready.

dpatte commented 7 years ago

under /server I have the following:

Meteor.publish('baseData', function() { return [File.find(), Catalog.find()]; });

under /lib I have the following:

Router.configure({ waitOn() { return Meteor.subscribe('baseData'); }, loadingTemplate: 'loading', layoutTemplate: 'layout', });

Router.route('/', { name: 'home', });

Router.route('/catalog/:_manu', { name: 'catalog', data() { // we have an issue here in that Catalog may not be loaded yet // because data() runs this function without checking waitOn first let manu = Catalog.find().fetch(); for (let i=manu.length; i--;) if (cleanURL(manu[i].name)==this.params._manu) return manu[i]; this.render("notFound"); }, });

When the subscription is ready (particularly after a page refresh) it shows not found template. but the data function works after that when selecting other pages.

chrisbutler commented 7 years ago

hey @dpatte, check out the guide info on subscriptions/waitOn http://iron-meteor.github.io/iron-router/#subscriptions

waiting on a subscription stops the route from being rendered, but it doesn't stop the data function from being called

also, your find().fetch() may be causing some weirdness/re-running of the hooks because they are reactive

dpatte commented 7 years ago

then what to do when the data() function is dependent on knowing data is available.

chrisbutler commented 7 years ago

i think you can just check this.ready()