【问题标题】:Meteor.user() Error: Uncaught TypeError: Cannot read property 'name' of nullMeteor.user() 错误:未捕获的 TypeError:无法读取 null 的属性“名称”
【发布时间】:2012-06-18 17:19:17
【问题描述】:

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

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

}

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

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

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

任何帮助将不胜感激。

【问题讨论】:

  • 什么是 Meteor.user()?您是否检查过它以查看其值(可能为 null)?
  • 它是 Meteor 提供的一个 javascript 对象。它返回没有 .name 属性的 [object Object]。
  • console.log 返回两行,null 和对象一个接一个。该对象包含所有预期的属性。

标签: javascript meteor


【解决方案1】:

如果没有用户登录,Meteor.user() 返回 null。所以为了安全起见,您应该执行类似 Meteor.user() ? Meteor.user().name : '' 的操作。

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2012-05-31
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2022-11-24
    • 2022-12-20
    • 1970-01-01
    相关资源
    最近更新 更多