开发者问题收集

TypeError:无法读取未定义错误的属性“map”

2020-12-22
53

我正尝试从我的 react 应用程序中的 API 获取数据。我有以下组件 ListApp

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

export default class ListApp extends Component{
    constructor(props){
        super(props);
        this.state = {
            items : [],
            isLoaded: false
        }
    }
    componentDidMount = () => {
        fetch('https://api.themoviedb.org/3/movie/550?api_key=my_api_key')
        .then(response => response.json())
        .then(response =>{
            this.setState({
                isLoaded: true,
                items: response.results
            })
            console.log(this.state.items)
        })
    }
    render(){
        var {isLoaded,items} = this.state;
        return(
           <div>
               {items.map(item => (<Card key={item.id} item={item} />))};
           </div> 
        )
    }
}

下面是我的 Card 组件:

import React,{Component} from 'react'

const Card = (props) => {
    const {items} = props;
    return(
        <div className="movie-container">
            <img src="https://image.tmdb.org/t/p/w185/{items.poster_path}" alt="NO PHOTO" className="movie-container__img"/>
            <div className="movie-container__about">
                <span className="movie-container__percent">{items.vote_average}</span>
                <h2 className="movie-container__title">{items.original_title}</h2>
                <p className="movie-container__date">{items.release_date}</p>
                <p className="movie-container__text">{items.overview}</p>
                <a href="https://www.themoviedb.org/movie/" className="movie-container__more">MORE</a>
            </div>
        </div>
    )
}

export default Card;

我收到错误 TypeError:无法读取未定义的属性“map”

我该如何解决?

3个回答

response.results 可能为空,因此您需要检查 items 是否为空。

{items && items.map(item => (<Card key={item.id} item={item} />))};
wangdev87
2020-12-22

如果您从服务器获取,一切都会好起来

{
  results: [
    {
      vote_average: ...
      original_title: ...
      release_date: ...
      overview: ...
    }
    ,
  ]
} 

{
  results: []
}

也修复它( items 将是 item ):

import React,{Component} from 'react'

const Card = (props) => {
    const {item} = props;
    return(
        <div className="movie-container">
            <img src="https://image.tmdb.org/t/p/w185/{item.poster_path}" alt="NO PHOTO" className="movie-container__img"/>
            <div className="movie-container__about">
                <span className="movie-container__percent">{item.vote_average}</span>
                <h2 className="movie-container__title">{item.original_title}</h2>
                <p className="movie-container__date">{item.release_date}</p>
                <p className="movie-container__text">{item.overview}</p>
                <a href="https://www.themoviedb.org/movie/" className="movie-container__more">MORE</a>
            </div>
        </div>
    )
}

export default Card;
Daniil Loban
2020-12-22

条件? true : false 如果我们想要呈现“正在加载...”文本:

它在 JavaScript 中有效,因为 true && 表达式始终计算为表达式,而 false && 表达式始终计算为 false。

这就是它应该这样写的原因:-

**items && items.map(item => (<Card key={item.id} item={item} />))};**
Amitesh mani tiwari
2021-01-09