Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

370
Vistas
Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'length')

I am making a movie app clone, but I am getting this error:

"Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'length')"

Error screenshot

import Image from 'next/image'
import { useEffect, useState } from 'react'
import { baseUrl } from '../constants/movie'
import { Movie } from '../typing'

interface Props {
  netflixOriginals: Movie[]
}

function Banner({ netflixOriginals }: Props) {
  const [movie, setMovie] = useState<Movie | null>(null)
 
  useEffect(() => {
    setMovie(
      netflixOriginals[Math.floor(Math.random() * netflixOriginals.length)]
    )
  }, [netflixOriginals])

  console.log(movie)
  
  return (
    <div>
      <div className='absolute top-0 left-0 h-[95vh] w-screen'>
      <Image
          src={`${baseUrl}${movie?.backdrop_path || movie?.poster_path}`}
          layout="fill"
        />
      </div>
    </div>
    )
}

export default Banner
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

netflixOriginals doesn't seem to be defined by you. Use optional chaining so that length property is not referenced if netflixOriginals is undefined.

  useEffect(() => {
    setMovie(
      netflixOriginals[Math.floor(Math.random() * netflixOriginals?.length)]
    )
  }, [netflixOriginals])
about 4 years ago · Juan Pablo Isaza Denunciar

0

netflixOriginals can be passed in as null. Defaulting it to an empty array would help but you still need to make sure you handle the case where the array has no values, meaning you have no shows to display.

import Image from 'next/image'
import { useEffect, useState } from 'react'
import { baseUrl } from '../constants/movie'
import { Movie } from '../typing'

interface Props {
  netflixOriginals: Movie[]
}

function Banner({ netflixOriginals = [] }: Props = {}) {
  const [movie, setMovie] = useState<Movie | null>(null)
 
  useEffect(() => {
    setMovie(
      netflixOriginals[Math.floor(Math.random() * netflixOriginals.length)]
    )
  }, [netflixOriginals])


  
  return (
   {
   movie 
   ? <div>
      <div className='absolute top-0 left-0 h-[95vh] w-screen'>
      <Image
          src={`${baseUrl}${movie?.backdrop_path || movie?.poster_path}`}
          layout="fill"
        />
      </div>
     </div>
   : <div>There are no movies</div>
    )
}

export default Banner

about 4 years ago · Juan Pablo Isaza Denunciar

0

It's possible that netflixOriginals is being passed in initially as null. You could test for a valid list before calling setMovie().

  useEffect(() => {
    if (netflixOriginals?.length) {
      setMovie(
        netflixOriginals[Math.floor(Math.random() * netflixOriginals.length)]
      )
    }
  }, [netflixOriginals])

This will also prevent your code from calling setMovie() when the array is empty.

The optional chaining operator (?.) will keep the first check of the length property from throwing an error if netflixOriginals is not defined.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda