TypeError:无法读取未定义的属性“ length”,无法修复

时间:2020-08-20 10:39:11

标签: reactjs axios web-deployment

 axios.get('https://api.covid19india.org/data.json')
      .then(response => {
        if (response !== undefined  && response.data.statewise.length>0) {
          self.setState({ data: response.data.statewise }, () => {
            self.getTestingData()
          })
        }
    })

当我在heroku上部署应用程序时,页面未加载(https://asciiclan-covid19.herokuapp.com/

显示无法读取未定义长度并参考上述代码。

1 个答案:

答案 0 :(得分:-1)

使用以下代码

import React , {useEffect, useState}from 'react';
import Axios from 'axios';

const Covid = () => {
    const [states, setStates] = useState([]);

    useEffect(() =>{
        
        Axios.get('https://api.covid19india.org/data.json').then(res => {
            if (res !== undefined  && res.data.statewise.length>0) {

               
                setStates(res.data.statewise );
            } 
        }    
        );
    } , []);

   const state = states.map((item, index) => {
       
       return(

           <p key = {index}>{item.state}</p>
       );
})

    return (
        <div>
            {state}
        </div>
    )
}

export default Covid