Home > Software engineering >  Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'length')
Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'length')

Time:07-13

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

CodePudding user response:

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])

CodePudding user response:

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

CodePudding user response:

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.

  • Related