简体   繁体   English

尝试映射数组时出现错误。 TypeError:无法读取未定义的属性“ map”

[英]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.

相关问题 TypeError:尝试读取通过props传递的数组时,无法读取未定义的属性“ map” - TypeError: Cannot read property 'map' of undefined, while trying to .map() an array passed through props 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 获取TypeError:无法读取未定义错误的属性“map” - Getting TypeError: Cannot read property 'map' of undefined error TypeError:无法读取未定义的属性“地图”错误 - TypeError: Cannot read property 'map' of undefined" error 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 类型错误:无法读取未定义的属性(读取“地图”)。 当我尝试通过一系列加密货币 map 时抛出错误 - TypeError: Cannot read properties of undefined (reading 'map'). Error being thrown when I am trying to map over an array of cryptocurrencies 在执行 react-redux 项目时出现此错误。 错误:- TypeError:无法读取 React-Redux 项目中未定义的属性“地图” - Getting this error while doing react-redux project. Error:- TypeError: Cannot read property 'map' of undefined in React-Redux project 尝试从数组中的 object 中的数组中获取 select 属性。 抛出类型错误:无法读取未定义的属性 - Trying to select property from array within object, within array. Thrown TypeError: Cannot read properties of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM