Meteor.user() 错误:未捕获的类型错误:无法读取 null 的属性“名称" [英] Meteor.user() Error: Uncaught TypeError: Cannot read property 'name' of null

查看:42
本文介绍了Meteor.user() 错误:未捕获的类型错误:无法读取 null 的属性“名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Meteor 框架,当我尝试将当前用户的名称返回给模板助手时出现此错误.

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>

我不断收到此错误:(错误:未捕获的类型错误:无法读取 null 的属性名称")

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

但是,在 javascript 控制台中一切正常.

However everything works fine from the javascript console.

任何帮助将不胜感激.

推荐答案

Meteor.user() 如果没有用户登录,则返回 null.因此,为了安全起见,您应该执行类似 Meteor.user() ?Meteor.user().name : ''.

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() 错误:未捕获的类型错误:无法读取 null 的属性“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆