I am working on a Meteor project and I need to access some information stored in a user's profile to make a map on the page. However, when I try to access Meteor.user(), I receive undefined because when the function is called, Meteor.user() has not been loaded.
Template.body.onRendered () ->
console.log Meteor.user()
address = Meteor.user()['profile']['address']
GoogleMaps.ready 'studyMap', (map) ->
# maps code that relies on address
Because Meteor.user() is not defined, I am not able to get the map to work. How can I wait for Meteor.user() to be defined?
I think your going to need an asynchronous callback to load the map only when address = Meteor.user()['profile']['address']
loads. As far as how to implement this, I'm not really sure.
One of my apps has to do something similar to wait on a subscription. I defined the waitOn() handler in my router.
Router.map(function(){
this.route('Brocator', {
path: '/',
template: 'brocator',
waitOn: function() {
return (Meteor.subscribe('people', this.params._gridId)
&& Meteor.subscribe('gridlog', this.params._gridId) );
},
data: function() {
var gridId = this.params._gridId;
return (People.find({gridId: gridId})
&& GridLog.find({gridId: gridId}));
},
fastRender: true
}),
... other routes deleted for clarity
})
You may decide to use something along the lines of
return Meteor.user() != 'undefined';
in your waitOn() handler.
Posted an answer for meteor/react here however the same can be done with blaze. I am not familiar with blaze but within your Template.body.onRendered
function you can listen to make sure that the login services are configured before rendering anything.
Tracker.autorun(() => {
if (Accounts.loginServicesConfigured()) {
// accounts is ready go ahead and render
} else {
// still waiting show loading spinner
}
});
I would recommend extracting this logic out to a higher level so that you don't keep repeating the same code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.