Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

375
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda