DEV Community

HAFIZ ABDULMANAN
HAFIZ ABDULMANAN

Posted on

?Error in Substring function in react

This render for the first time but soon I reload the page I get the error Slice is not defined.

I am getting a random movie Object from API. Object.data.results[any random index].overview is description of movie. I wanna cut this description to only 10 (lets say) letters.



function App() {

  const [movie, setMovie] = useState([])

  useEffect(() => {
    async function fetchData() {
      const req = await axios.get("https://api.themoviedb.org/3/movie/550?api_key={API_KEY})
//example IMDB API key

setMovie(() =>
req.data.results
[Math.floor(Math.random() * req.data.results.length)]
)
    }
//getting a random movie from 20 movies by data.result
    fetchData();
  }, [])

console.log(movie?.overview.slice(0,15))//working 


  return (
    <div>
        <h1>{movie?.title || movie?.name || movie?.original_name}</h1>

        <div>
          {movie?.overview.slice(0,15)} //error
        </div>
    </div>    
  )
}

export default App


Enter fullscreen mode Exit fullscreen mode

Top comments (3)

 
hamsof profile image
HAFIZ ABDULMANAN

Thanks Brother !

Collapse
 
hamsof profile image
HAFIZ ABDULMANAN

I am still getting that error even on slice!

ThankYou very much for your reply. It really means a lot. I have updated my question that was actually my problem and my code.

Collapse
 
hamsof profile image
HAFIZ ABDULMANAN

Please answer this as well, I really appreciate !