简体   繁体   English

TypeError 从 Api 获取数据时无法读取未定义的属性“映射”

[英]TypeError Cannot read property 'map' of undefined when getting data from Api

I am trying to get get some data from an API using React.我正在尝试使用 React 从 API 获取一些数据。 Below is one of my components where I am getting the error.下面是我收到错误的组件之一。

import React, { Component } from "react";
import Filmcard from "./Filmcard";

const url = "http://www.omdbapi.com/?s=star&apikey=4ababda2";

class Filmcardlist extends Component {
  constructor() {
    super();
    this.state = {
      films: []
    };
  }
  componentDidMount() {
    fetch(url)
      .then(resonse => resonse.json())
      .then(data => {
        this.setState({ films: data });
      })
      .catch(error => console.log("I have errored" + error));
  }

  render() {
    return (
      <div>
        {this.props.films.map(film => (
          <Filmcard
            Title={film.Title}
            Year={film.Year}
            imdbID={film.imdbID}
            Type={film.Type}
          />
        ))}
      </div>
    );
  }
}

export default Filmcardlist;

I am fairly new to React and I'm not sure why I am getting this error.我对 React 相当陌生,我不确定为什么会收到此错误。

change:改变:

this.setState({ films: data });

to:到:

this.setState({ films: data.Search });

and this.props.films to this.state.films in render function.this.props.filmsthis.state.films在渲染函数中。

Just change props with state and as I read the comments you need to map data.search.只需更改带有状态的道具,当我阅读您需要映射 data.search 的评论时。 Try this code, hope I helped!试试这个代码,希望我有所帮助!

{
if(this.state && this.state.films && this.state.films.search){
this.state.films.search.map(film => (
      <Filmcard
        Title={film.Title}
        Year={film.Year}
        imdbID={film.imdbID}
        Type={film.Type}
      />
    ))}
}

暂无
暂无

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

相关问题 尝试从 MediaWiki API 获取一条数据时出现“类型错误:无法读取未定义的属性 '0'” - Getting 'TypeError: Cannot read property '0' of undefined' when attempting to get a piece of data from MediaWiki API 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined TypeError:从 API 获取时无法读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined while fetching from API 类型错误:当我尝试访问 API 调用数据时,无法读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined when I am trying to access API call data TypeError:检索API数据时无法读取未定义的属性“ temp” - TypeError: cannot read property “temp” of undefined when retreiving API data 我不断收到此类型错误:无法读取未定义的属性“地图” - I keep getting this TypeError: Cannot read property 'map' of undefined 获取TypeError:无法读取未定义错误的属性“map” - Getting TypeError: Cannot read property 'map' of undefined error TypeError:无法读取未定义 React API 的属性“地图” - TypeError: Cannot read property 'map' of undefined React API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM