1

I am trying to get all movies but the API only returns first 10 movies. Any help would be appreciated.

async getResults(page = 1) {
    const apiKey = '#######';
    const proxy = 'http://cors-anywhere.herokuapp.com/';
    try {

        while (page <= 5) {
            const res = await axios(`${proxy}http://www.omdbapi.com/? apikey=${apiKey}&s=${this.query}&type=movie&page=${page}`);
            this.result = res.data;
            this.page = page;
            page++;
        }

    } catch (error) {
        console.log(error);
    }

} 
hamid
  • 21
  • 1
  • 7

1 Answers1

1

OMDB API has pagination, which means it will return a set number of movie response data in each request. In this case it is returning 10 movies per request. You have to pass a query parameter page=2 if you want to get the data for the second page, because page is not a required query parameter and defaults to 1.

check the official OMDB docs for more info

vanshaj
  • 499
  • 1
  • 4
  • 13
  • I have wrapped my code in while loop and pass the page parameter and then incrementing the page by 1 on each request but still returns the first 10 movies. while (page <= 5) { const res = await axios(`${proxy}http://www.omdbapi.com/?apikey=${apiKey}&s=${this.query}&type=movie&page=${page}`); this.result = res.data; this.page = page; page++; } – hamid Oct 10 '20 at 13:35
  • I don't see any page query parameter in the URL you provided in the question. Can you post the URL you are using and the surrounding loop in the post above ? – vanshaj Oct 10 '20 at 13:43
  • const res = await axios(`${proxy}http://www.omdbapi.com/?apikey=${apiKey}&s=${this.query}&type=movie&page=${page}`); – hamid Oct 10 '20 at 13:49
  • Please edit your question and update the API url. Also add the code block which calls the getResults function – vanshaj Oct 10 '20 at 14:21
  • I call the method in my controller. try { //4-SEARCH FOR MOVIES await state.search.getResults(page); } catch (error) { console.log(error) } } – hamid Oct 10 '20 at 14:28