小编Yat*_*ngh的帖子

TypeError:无法读取未定义的属性(读取“地图”)React JS

我面临着错误TypeError: Cannot read properties of undefined (reading 'map')

此代码应该返回卡片标题中的城市名称,该卡片标题是在我的其他文件中定义的,但会引发错误。

代码:

import React, {Component} from 'react';
import Body from './Body';

class Weather extends Component {
  constructor(props) {
    super(props);
    this.state = {
      weather: [],
    };
  }

  async componentDidMount() {
    const url = `http://api.weatherapi.com/v1/current.json?key=${this.props.api}&q=Jaipur&aqi=no`;
    let data = await fetch(url);
    let parsedData = await data.json();
    this.setState({
      weather: parsedData.weather,
    });
  }

  render() {
    return (
      <div className="container">
        <div className="row">
          {this.state.weather.map((element) => {
            return (
              <div className="col-md-4">
                <Body city={element.location.name} />
              </div>
            );
          })}
        </div>
      </div> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×1

reactjs ×1