简体   繁体   English

ReactJS TypeError:无法读取未定义的属性

[英]ReactJS TypeError: Cannot read property of undefined

I have this code right here, that works in this online text editor: https://codesandbox.io/s/o4n0v203vy 我在这里有此代码,可在此在线文本编辑器中使用: https : //codesandbox.io/s/o4n0v203vy

In the npm console , everything is okay: npm控制台中 ,一切正常:

在此处输入图片说明

But in the browser , this happens: 但是在浏览器中 ,会发生这种情况:

在此处输入图片说明

What logs in the console, when i do export var renderTree = (node, setActiveNode, activeNode) => { console.log(node && node.type, 'look here'); 当我export var renderTree = (node, setActiveNode, activeNode) => { console.log(node && node.type, 'look here');

在此处输入图片说明

I can't figure out why is this happening. 我不知道为什么会这样。 Any help is appreciated! 任何帮助表示赞赏! Thanks! 谢谢!

The variable node is undefined so doesn't have a property called type . 变量nodeundefined因此没有名为type的属性。

Change your check to 将支票更改为

if(node && node.type === 'file'){
    ...
}

您可能正在浏览器中运行源文件,而不是已构建的文件。

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

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