Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!