简体   繁体   English

React.js - “无法读取未定义的属性‘地图’”

[英]React.js - “Cannot read property 'map' of undefined”

I have tried the methods that I found by searching but none of that work?我已经尝试了通过搜索找到的方法,但这些方法都不起作用? Why it's still showing that map is undefined?为什么它仍然显示map未定义?

import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

class ComboBox extends React.Component {

  state ={
    option : []
  }
  async componentDidMount(){
  const url = await 'https://api.covid19india.org/data.json';
  const response =await fetch(url);
  const data = response.json();
  const pop = data.statewise.map(st=>st.state);
  this.setState({option : pop})

  }


  render(){
    return (
    <Autocomplete
      id="combo-box-demo"
      options={(this.state.option)}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
    />
  );}
}

export default  ComboBox```



response.json() return a promise . response.json()返回一个promise you should change the line你应该换行

  const data = response.json();

to

  const data = await response.json();

And, by the way, using await in const url = await 'https://api.covid19india.org/data.json';顺便说一句,在const url = await 'https://api.covid19india.org/data.json';中使用await is redundant是多余的

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

相关问题 错误:无法读取未定义 React.JS 的属性“map” - Error : Cannot read property 'map' of undefined React.JS 无法读取未定义 [React.js] 的属性“地图” - Cannot read property 'map' of undefined [React.js] TypeError:无法读取 react.js 中未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined in react.js React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined React.js-无法读取未定义的属性“ map” - React.js - Cannot read property 'map' of undefined TypeError:无法读取未定义React.js的属性“ map” - TypeError: Cannot read property 'map' of undefined React.js “无法读取未定义(React.js)的属性&#39;map&#39;” - “Cannot read property 'map' of undefined (React.js)” TypeError:无法读取React.js上未定义的属性&#39;map&#39; - TypeError: Cannot read property 'map' of undefined on React.js 函数返回数据以过滤和映射显示错误 React.js TypeError:无法读取未定义的属性“val” - function returning the data to filter and map that show error React.js TypeError: Cannot read property 'val' of undefined TypeError:无法读取 Netlify CMS/React.js 上未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined on Netlify CMS/React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM