0

I am trying to get data from an API. If api call is returning more pages then I am making multiple calls. So, I did something as below. Though, it's printing data but not adding it to Title array. It's just getting the first page data but when I am trying to get more data to make different GET request it's not letting me do it. If anyone know the reason please help.

const axios = require("axios");

let pageNumber = 1;
let title = [];

let valueToSearch = 'love'
let url = `https://jsonmock.hackerrank.com/api/movies/search/?Title=${valueToSearch}`;
let pageURL = `https://jsonmock.hackerrank.com/api/movies/search/?Title=love&page`;

const getMovieName = async url => {
    try{
        const response = await axios.get(url);
        const data = response.data;

        let totalPages = data.total_pages;

        let mainData = data.data;

        mainData.forEach(element => {
            let movieName = element.Title;
            title.push(movieName);
        });

        for(var i=2; i <= totalPages; i++){

            let newUrl = `${pageURL}=${i}`;

            const getMovieNameInner = async url => {
                console.log(url);
                try{
                    const response = await axios.get(newUrl);
                    const data = response.data;
                    let mainData = data.data;

                    let perPage = data.per_page;

                    mainData.forEach(element => {
                        let movieName = element.Title;
                        console.log(`printing inside ${movieName}`);
                        title.push(movieName);
                    });

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

            getMovieNameInner(newUrl);
            console.log(title.length);
        }

        console.log(title);

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

getMovieName(url);
Atur
  • 1,712
  • 6
  • 32
  • 42
abankitbaid
  • 137
  • 5
  • 15
  • Without fully checking your code, most likely this is a problem due to the async operation you are doing inside the for loop. You could solve the problem using some solutions provided here: https://stackoverflow.com/questions/7696747/call-asynchronous-function-inside-for-loop – enf0rcer Apr 02 '18 at 19:47
  • Hi @enf0rcer i tried that but not working. Just want to know if a for loop can caused async call. I dont want to use ForEach because thats not fulfilling my requirement. If you find any solution related to this please let me know. Thanks again – abankitbaid Apr 02 '18 at 19:51
  • All you need to do is put `await` in front of `getMovieNameInner(newUrl);`. Although that will make each request happen in series. The best thing you can do is probably collect all the Promises that `getMovieNameInner(newUrl);` returns into an array called `promises` and then `await Promise.all(promises) ;` after the loop. – Paul Apr 02 '18 at 19:52
  • Thanks @Paulpro. I missed it. Thanks a lot. it was a great help. – abankitbaid Apr 02 '18 at 19:56
  • You're welcome. – Paul Apr 02 '18 at 19:56

0 Answers0