[英]Getting error while trying to map over array. TypeError: Cannot read property 'map' of undefined
So for the practice I am trying to consume rest point with react framework. 因此,对于该实践,我尝试使用react框架消耗休息点。 But somehow I getting error like:
但是以某种方式我得到错误,如:
TypeError: Cannot read property 'map' of undefined
Json look like this: 杰森看起来像这样:
{
"name":"sadasd.tx",
"application":"Word",
"id":"123",
"type":"Mixed",
"containers":[
"Variant1",
"Variant2_sch",
"Variant5",
"Variant6",
"Variant3",
"Variant4"
]
}
React component looks like this: React组件如下所示:
class ContainerList extends React.Component{
constructor(props){
super(props);
this.state={fileContent: []};
}
componentDidMount(){
fetch('http://localhost:8080/'+ this.props.file)
.then(res => res.json())
.then((data) => {
this.setState({fileContent: data})
})
}
render() {
const containerNames = this.state.fileContent.containers.map(container => <Container containerName={container} />);
return (
<table>
<tbody>
{containerNames}
</tbody>
</table>
)
}
}
After removing map over array and debugging in browser I see that json object is correctly assigned to the variable. 删除数组上的映射并在浏览器中调试后,我看到json对象已正确分配给该变量。 But when iterating over it I getting undefined error.
但是,当对其进行迭代时,我得到了未定义的错误。 Anyone has idea what might be wrong?
任何人都知道什么可能是错的吗?
[EDIT] I am completely does not understand this. [编辑]我完全不明白这一点。 I did understand why does it throwing this error, after your posts guys.
我确实了解,为什么在您的发布人员之后会引发此错误。 But I did something like this after reading your answers:
但是在阅读您的答案后,我做了这样的事情:
I added to state
another variable isLoading
with default true value. 我加入
state
另一个变量isLoading
默认真正的价值。 And in fetch after getting json I set it to false. 并在获取json后将其设置为false。
fetch('http://localhost:8080/'+ this.props.xx)
.then(res => res.json())
.then((data) => {
this.setState({fileContent: data, isLoading: false})
})
and in render method I did add if statement: 在render方法中,我确实添加了if语句:
render() {
let containerNames;
if(this.state.isLoading){
containerNames = <h1>Loading</h1>
}else{
containerNames = this.state.fileContent.listContainers.map(listContainer => <Container listContainerName={listContainer} />);
}
return (
<table>
<tbody>
{containerNames}
</tbody>
</table>
)
So it should not be possible to enter else statemend before fetchind data. 因此,在获取数据之前应该不可能输入else语句。 But react is somehow doing this.
但是反应就是这样做。 And throwing error.
并抛出错误。
During initial render value of this.state.fileContent.containers
will be undefined, and undefined
do not have map
method 在
this.state.fileContent.containers
初始呈现期间,值将是未定义的,并且undefined
没有map
方法
You add check before map
您在
map
前添加支票
const containerNames = Array.isArray(this.state.fileContent.containers) && this.state.fileContent.containers.map(
const { fileContent } = this.state
const { containers } = fileContent
const containerName = containers ? containers : []
like the other answers mentioned, containers doesn't exist during the initial render. 像提到的其他答案一样,容器在初始渲染期间不存在。 here i did some desconstructing for personal preferences, but i'm just stating if containers exist, then map over it, otherwise map over an empty array
在这里,我针对个人喜好进行了一些解构,但是我只是在说明容器是否存在,然后在其上进行映射,否则在一个空数组上进行映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.