简体   繁体   English

我正在尝试通过从 TMDB API 加载的数组来 map

[英]I am trying to map through array loaded from TMDB API

I uploaded my Data using FETCH from the TMDB API, When i try to map through it, it gives me an error undefined is not an object (evaluating 'NowMovies.map')我使用 TMDB API 中的 FETCH 上传了我的数据,当我尝试通过它访问 map 时,它给了我一个错误 undefined is not an object (evaluating 'Moviemap')

this is my code:这是我的代码:


    constructor(props){
    super(props);
    this.state={
      NowMovies:[],
      isLoaded:false
    }
  }

    componentDidMount(){
     fetch('API')
    .then(data => data.json())
    .then(({MoviesLists}) => {this.setState({
      isLoaded:true,
      NowMovies:MoviesLists
    })
    })
    }
  render(){
    var {isLoaded,NowMovies} = this.state
    if (!isLoaded){
      return <div> .... Loading</div>
    }
    else{
    return(
      <div>

      <ul>  
      {NowMovies.map(Movie=>(<li key={NowMovies.id}> name:{NowMovies.original_title}</li>
      ))}
      </ul>
      </div>



this is what the API loades这就是 API 加载的内容

can anyone help me with loading this data to my array and map through it??谁能帮我将这些数据加载到我的阵列和 map 通过它?

You're trying to access NowMovies within the map function, you should use the Movie object you've passed instead.您正在尝试访问NowMovies中的 NowMovies,您应该改用您通过的Movie object。

 {NowMovies.map(Movie =>( <li key={Movie.id}> name:{Movie.original_title} </li> }

You can not assign directly the value, for that you can use.concat() or.push().您不能直接分配值,因为您可以使用.concat() 或.push()。 And correct with.map().并用.map() 更正。

constructor(props){
    super(props);
    this.state={
      NowMovies:[],
      isLoaded:false
    }
  }

    componentDidMount(){
     fetch(API)
    .then((data) => data.json())
    .then(({MoviesLists}) => {
  let movieList = this.state.NowMovies.concat(MoviesLists);
this.setState({
      isLoaded:true,
      NowMovies : movieList
    })
    })
    }
  render(){
    var {isLoaded,NowMovies} = this.state
    if (!isLoaded){
      return <div> .... Loading</div>
    }
    else{
    return(
      <div>
      <ul>  
      {NowMovies.map(Movie=>(<li key={Movie.id}> name:{Movie.original_title}</li>
      ))}
      </ul>
      </div>

暂无
暂无

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

相关问题 尝试通过 api 中的数组 to.map 时收到错误 - I receive an error when attempting to .map through an array from an api 如何从 TMDB API 中提取更多数据? - How do I pull more data from the TMDB API? 我正在尝试将 JSON 数据的值存储在来自 servlet 的数组中,但它显示帖子。map 不是 function - I am trying to store value of JSON data in an array from servlet but it shows posts.map is not function 我正在尝试从 2 个数组中检索结果 - I am trying to retrieve result from 2 array 我试图遍历对象数组并将其填充到表中 - I am trying to loop through an array of objects and populate them to a table 我正在尝试通过复选框从数组中删除多个元素我正在使用 vue.js 但我无法弄清楚该怎么做 - I am trying to delete multiple elements from an array through checkbox i am using vue.js but i am unable to figure out how to do it 我正在尝试从对象数组中检索数字数组 - I am trying to retrieve an array of numbers from an array of object 试图缩短我从 API 调用中得到的响应 - Trying to shorten the response I am getting from a API call 我正在尝试使用天气api,我正在尝试从中获取数据但未定义但属性确实存在 - I am trying to use a weather api that I am trying to pull data from i am getting undefined but the property does exist 我正在尝试从图像数组中显示图像 - javascript - I am trying to show an image from an array of images - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM