Meteor.user (). Profile returns 'profile' undefined

I have an assistant called 'isActive' and a template called 'create' .. see below

Template.create.isActive = function () { return Meteor.user().profile.isActive; }; 

When I try to run this code, it returns the following in the console: "Exception in the template helper: TypeError: cannot read the profile property" undefined ".

Basically, I want to output the "isActive" information from the current user profile and return it to the template. Any idea why this is not working?

Update

 //startup on server side: Meteor.publish("userData", function() { if (this.userId) { return Meteor.users.find({_id: this.userId}, {fields: {'profile.isActive': 1}}); } else { this.ready(); } }); //startup on client side Meteor.subscribe('userData'); //router this.route('list', { path: 'list', waitOn : function () { return Meteor.subscribe('userData'); }, data : function () { return Meteor.users.findOne({_id: this.params._id}); }, action : function () { if (this.ready()) { this.render(); } } }); 
+5
source share
1 answer

Meteor.user () - undefined. You are either not logged in with any user, or your template is displayed before the user collection is synchronized with the client. If you use a router, such as an iron router, you can wait until the collection is available or for the user to log in.

Without using a router, the simplest would be to check if the user is defined:

 Template.create.isActive = function () { return Meteor.user() && Meteor.user().profile.isActive; } 

Since Meteor.user () is reactive, the template will reload when the user changes, i.e. when it will be available.

This is a common mistake by the way, see also:

+9
source

Source: https://habr.com/ru/post/1201252/


All Articles