Reputation: 869
i'm completely new to MeteorJS, just looking at demos and poking around. it is possible that i'm just missing something really basic. i'm using meteor with angular-ui-router. if i start with a state that loads data into a controller, i run into issues because DDP has not initialized yet. this is similar to what was described in Meteor: How can I tell when the database is ready?
relevant part of routing config:
resolve: {
location: ($stateParams) => {
console.log('find', $stateParams);
let handle = setInterval(function () {
console.log(Meteor.status().connected, Meteor.status().status);
console.log( !!Locations.findOne({code: $stateParams.code}) ? 'found' : 'not found' );
}, 100);
setTimeout(function () {
clearInterval(handle);
}, 2000);
console.log(Meteor.status().connected, Meteor.status().status);
return Locations.findOne({code: $stateParams.code});
}
}
and the output is
false "connecting"
false "connecting"
not found
true "connected"
not found
true "connected"
not found
true "connected"
found
true "connected"
found
my question is: do i understand correctly that the problem here is that for things to work as expected they must happen in this sequence:
and 4. is running before 3? if so, what is the suggested workaround?
cross-post: https://github.com/meteor/meteor/issues/9127
Upvotes: 0
Views: 36
Reputation: 20226
Whenever you subscribe in Meteor you should check to see if the subscription is ready before rendering the results. Typically you'd show a spinner when it's not.
const mySub = Meteor.subscribe('somePublication');
if (mySub.isReady()) {
... you can access the data using minimongo
} else {
... not so fast Jack
}
DDP's websocket connection needs to be up before pub-sub can transfer data but that's not a very significant source of delay in most cases.
You can catch the event when a publication becomes ready in a Tracker.autorun()
:
const mySub = Meteor.subscribe('somePublication');
Tracker.autorun(() => {
if (mySub.ready()) ... do something
});
OR in a callback from the subscription:
Meteor.subscribe('somePublication',() => {
... subscription is ready
});
Upvotes: 2