Getting an empty array in React while fetching the data from the api

hellraiser999

I'm fetching from the moviedb api and working on searching movies. I've taken some reference from alligator.io tutorial. Below is my code of the Header component where i am fetching the data.

Header.js

import React, { Component } from "react"
import { Navbar, Button, Form, FormControl } from "react-bootstrap"
import { NavLink } from "react-router-dom"
import axios from "axios"
const apiKey = process.env.REACT_APP_MOVIE_DB_API_KEY


class Header extends Component {
  state = {
    isSearching: false,
    value: "",
    movies: []
  }

  searchMovies = async val => {
    console.log("val", val)
    this.setState({ isSearching: true })
    const res = await axios(
      `https://api.themoviedb.org/3/search/movie?api_key=${apiKey}&language=en-US&query=${val}page=1&include_adult=true`
    )
    const movies = await res.data.results
    console.log(movies)
    this.setState({ movies: movies, isSearching: false })
  }

  handleChange = e => {
    const { name, value } = e.target
    this.searchMovies(value)
    this.setState({
      [name]: value
    })
  }

  handleSearch = () => {
    console.log(this.state.movies)
  }

  render() {
    return (
      <div>
        <Navbar
          bg="dark"
          expand="lg"
          style={{ justifyContent: "space-around" }}
        >
          <NavLink to="/">
            <Navbar.Brand>Movie Catalogue</Navbar.Brand>
          </NavLink>
          <Navbar.Toggle aria-controls="basic-navbar-nav" />
          <Navbar.Collapse id="basic-navbar-nav">
            <Form inline>
              <FormControl
                type="text"
                placeholder="Search"
                className="mr-sm-2"
                onChange={this.handleChange}
                name="value"
                value={this.state.value}
              />
              <Button variant="dark" onClick={this.handleSearch}>Search</Button>
            </Form>
          </Navbar.Collapse>

          <NavLink to="/popular">Popular</NavLink>
          <NavLink to="/now-playing">Now Playing</NavLink>
          <NavLink to="/top-rated">Top Rated</NavLink>
          <NavLink to="/upcoming">Upcoming</NavLink>
        </Navbar>
      </div>
    )
  }
}

export default Header

I am getting an empty array in movies. When I console log this.state.movies inside handleSearch it's stil returning an empty array. I don't know why's it happening. The url and everything is correct but I'm getting an empty array.

VJR08

You are not calling appropriate method of axios. Use get() method

Try this:

 searchMovies = async val => {
    this.setState({ isSearching: true })
    const res = await axios.get(
      `https://api.themoviedb.org/3/search/movie?api_key=${apiKey}&language=en-US&query=${val}page=1&include_adult=true`;
    )
    const movies = await res.data.results;
    this.setState({ movies: movies, isSearching: false })
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

react functional component is getting called 3 times while fetching data from api async way

Getting keyerror while fetching JSON data from API using python

Getting error autocomplete is not a function while fetching data from api?

Gettin 'null' while fetching data from api with React Context

Fetching data from api in react

Fetching data from nested array in API React Native

applying *ngFor on <li> only shows list with empty data, while fetching data from api on ngOnInit()?

Trouble while fetching data from API in Angular

Fetching data from api promise in react API

Getting 403 status while fetching data from ASP.Net Web Api

Why am I getting an empty array and incorrect data fetching in console?

Getting data from mongoose on server but an empty array in React

I am getting error while fetching data in react-native

Array is sometimes empty in Vue.js if getting data from API

Why I am getting undefined while fetching data form API

Fetching data from YouTube API with React Hooks

React Router Fetching data from API

Fetching data from api and passing it to state in React

Android Mokito Testing Room DAO class. Gettting empty array while fetching data from db while testing

getting incorrect values while fetching values from an array

Data's not displaying while fetching from API and using Flatlist in react native

FIREBASE: getting null while fetching the data from database

Getting [jwt is not defined] while fetching data from MongoDB backend -

Getting Encryption Error while parsing data fetching from Salesforce

React useEffect returns empty array after fetching data with axios

Fetching External API data with React: this.state is empty

Fetching API data with React

Issues while Fetching API data in my react component

Error While Fetching Data From Google Analytics API from R

TOP Ranking

HotTag

Archive