开发者问题收集

突然出现 Uncaught TypeError: Cannot read properties of undefined (reading '0') 的错误,而之前相同的代码运行正常

2023-04-20
892

我已经创建了一个 spring API,并在 Hero.js 文件中调用它来实现 Carousel 效果。直到昨晚它都运行良好。

但是今天当我重新启动我的项目时[它显示错误

Uncaught TypeError: Cannot read properties of undefined (reading '0')

我不明白错误发生在哪里。

Hero.js

import './Hero.css';
import Carousel from 'react-material-ui-carousel';
import { Paper } from '@mui/material';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCirclePlay } from '@fortawesome/free-solid-svg-icons';
import {Link, useNavigate} from "react-router-dom";
import Button from 'react-bootstrap/Button';
import MovieData from './MovieData';


const Hero = ({movies}) => {

    const navigate = useNavigate();

    function reviews(movieId)
    {
        navigate(`/Reviews/${movieId}`);
    }

  return (
    <div className ='movie-carousel-container'>
      <Carousel>
        {
            movies?.map((movie) =>{
                return(
                    <Paper key={movie.imdbId}>
                        <div className = 'movie-card-container'>
                            <div className="movie-card" style={{"--img": `url(${movie.backdrops[0]})`}}>
                                <div className="movie-detail">
                                    <div className="movie-poster">

                                        {/* movie poster to movie details */}
                                        <img 
                                            variant ="info"
                                            onClick={() => reviews(movie.imdbId)}
                                            src={movie.poster}
                                            alt=""
                                        />
                                    </div>
                                    <div className="movie-title">
                                        <h4>{movie.title}</h4>
                                    </div>
                                    <div className="movie-buttons-container">
                                        <Link to={`/Trailer/${movie.trailerLink.substring(movie.trailerLink.length - 11)}`}>
                                            <div className="play-button-icon-container">
                                                <FontAwesomeIcon className="play-button-icon"
                                                    icon = {faCirclePlay}
                                                />
                                            </div>
                                        </Link>

                                        <div className="movie-review-button-container">
                                            <Button variant ="info" onClick={() => reviews(movie.imdbId)}>Reviews</Button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </Paper>
                )  
            })
        }
      </Carousel>


        {/* Movie Details with Watchlist and Booking Button */}
        <div className='home'>
            <MovieData/>
        </div>
    </div>

  )
}

export default Hero
2个回答

尝试使用 Elvis 运算符 ?. 进行可选链接以获取对象中的对象。

<div className="movie-card" style={{"--img": `url(${movie?.backdrops[0]})`}}>

<div className="movie-card" style={{"--img": `url(${movie?.backdrops?.[0]})`}}>
Unknown Beginner
2023-04-21

错误表明错误发生在文件的第 28 行,即以下行:

<div className="movie-card" style={{"--img": `url(${movie.backdrops[0]})`}}>

这表明无法在索引 0 处访问带有背景数组的电影对象(因此背景未填充)。如果不查看其余代码,很难知道原因,但请检查渲染 Hero 组件的组件并确保所有电影对象都包含背景数组。

Tomr93
2023-04-20