After logout and login I try to click something on another page but see endless spinner due to userId null. How to sync Meteor.userId after changing user? ON server side:
Meteor.publish('myPublish', function () {
if (!this.userId) {
return this.ready();
}
return Collection.find();});
On client side:
const handles = [
Meteor.subscribe('settings')
];
const loading = handles.some(handle => !handle.ready());
The
Meteor.userIdfunction is a reactive datasource, which allows you to autorun a Tracker and subscribe, when the userId is there.Classic Meteor code would look like this:
For React you use
withTrackerand should include theMeteor.userIdto your bindings:The call to
Meteor.userIdshould activate the internal Tracker computation to re-run a cycle, once it returns a different value (a non-null value, once logged in).You can also use
Meteor.loggingInas reactive data source:References:
https://docs.meteor.com/api/tracker.html#Tracker-autorun
https://guide.meteor.com/react.html#using-withTracker
https://docs.meteor.com/api/accounts.html#Meteor-userId
https://docs.meteor.com/api/accounts.html#Meteor-loggingIn