简体   繁体   English

使用解析的字符串时,为什么会出现错误“无法读取null的属性'名称'”?

[英]Why am I getting error 'Cannot read property 'name' of null' when using a parsed string?

Here I have a react container that holds an object in a variable called strLoc: 在这里,我有一个React容器,该容器将对象保存在名为strLoc的变量中:

(what var strLoc looks like console logged) (var strLoc看起来像控制台记录的一样)

Object {name: "Burj Khalifa", locationLat: "25.197787", locationLong: "55.274862"} 对象{名称:“哈利法塔”,位置纬度:“ 25.197787”,位置长度:“ 55.274862”}

I want to use the name property of the object, but when I try to get {strLoc.name}, I get an error message: 我想使用对象的名称属性,但是当我尝试获取{strLoc.name}时,出现错误消息:

Uncaught TypeError: Cannot read property 'name' of null 未捕获的TypeError:无法读取null的属性“名称”

I don't understand why I can't just pull off the property values to use them? 我不明白为什么我不能仅仅提取属性值来使用它们? Does the problem have something to do with that the object was a string before (I parsed the string and saved the resulting object in var strLoc) ? 问题是否与对象之前是字符串有关(我解析了字符串并将结果对象保存在var strLoc中)?

here is my code: 这是我的代码:

class NoteOutput extends React.Component {
    render() {
        var strLoc = JSON.parse(this.props.location); 
        return(
            <div>
                <div>hello</div>
                <div>{strLoc.name}</div>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        location: state.selectedLocation
    };
}

its probably because you have the parse in your render() and it tries to show .name of a string/before it gets parsed. 这可能是因为您的render()中有解析器,并且它试图在解析之前显示字符串的.name。

try this: 尝试这个:

{strLoc && <div>{strLoc.name}</div>}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 为什么在使用getElementById时出现“ TypeError:无法读取null的属性&#39;value&#39;”的信息? - Why am I getting “TypeError: Cannot read property 'value' of null” when using getElementById? 为什么我无法读取 null javascript 的属性“addEventListener” - why i am getting Cannot read property 'addEventListener' of null javascript 为什么我收到 null 的“无法读取属性”“addEventListener” - Why am i getting 'Cannot read property 'addEventListener' of null 为什么我使用Knockout JS获得“无法读取属性'nodeType'的null”错误? - Why am I getting a “Cannot read property 'nodeType' of null” error with Knockout JS? 为什么在尝试通过 id 更改 div 样式时无法读取 null 的属性“样式”? - Why am I getting Cannot read property 'style' of null when trying to change div style by id? 无法将属性“ innerHTML”设置为null,为什么会出现此错误 - Cannot set property 'innerHTML' of null why am I getting this error 我正在尝试将字符串与特定单词匹配,但出现错误 cannot read property input of null in else - I am trying to match a string with particular word, but i am getting an error cannot read property input of null in else 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM