简体   繁体   中英

Meteor.user() Error: Uncaught TypeError: Cannot read property 'name' of null

Im working with the Meteor framework and came by this error when I tried to return the name of the current user to a template helper.

Template.user.userName = function (){
    return Meteor.user().name;

}

<template name ="user">
    {{userName}}
</template>

I keep getting this error: (Error: Uncaught TypeError: Cannot read property 'name' of null)

However everything works fine from the javascript console.

Any help would be much appreciated.

Meteor.user() returns null if there is no user logged in. So to be safe you should do something like Meteor.user() ? Meteor.user().name : '' Meteor.user() ? Meteor.user().name : '' .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM