Cannot read property 'profile' of undefined in meteor.JS function?


#1
if (Meteor.isClient) {
console.log(Meteor.user().profile.name);
}

Why does not this code?

Uncaught TypeError: Cannot read property ‘profile’ of undefined


#2

fix

if (Meteor.isClient) {
    
        Meteor.startup(function () {
       
console.log(Meteor.user().profile.name);
    });
  }

#3

Also, there is a chance that name will be undefined as well which will crash your templates. Just to ease development I usually do checks for nested objects so they don’t return undefined.