How to get all movies from OMDBAPI

This is how my class is structured and I want to append the API response to the result property. I want to store the movies in object property ‘this.result’ not in array. Is there anyway I can append the api response to ‘this.result’ everytime I make request instead of storing in array.

import axios from 'axios';

export default class Search {
    constructor(query) {
        this.query = query
    }

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

            while (page <= 3) {
                const res = await axios(`${proxy}http://www.omdbapi.com/?apikey=${apiKey}&s=${this.query}&type=movie&page=${page}`);
                this.result = res.data;
                this.result.Search.forEach(movies => moviesList.push(movies))
                page++;
            }
            console.log(moviesList);

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

    }
}

isn’t this.result an array? can’t you append the results to that array?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

this.result is where the api response is stored and it has value called Search that is an array

A property contains a single value, be it a number, a string, an object, an array, etc, it can not contain multiple values. You can not assign a bunch of objects to a single property. You can assign an object, or an array to a property, and that object or array can contain multiple values. Just assign the array to the property.

If you are saying you want it to be an object and not an array, I’m not sure why you would want nested objects. The Search property is already an array of objects so why not keep it like that? If you plan on doing any sort of iteration over the data (like a map) I’d suggest you use an array of objects and not an object of objects.

Do you mean assign the moviesList array to this.result property of the object. Thanks for help.[quote=“lasjorg, post:24, topic:423905”]
Just assign the array to the property.
[/quote]

I think its working now. I am looping through the this.result.Search property and then push it in MoviesList array and eventually reassign it to this.result.

try { while (page <= 3) { const res = await axios({proxy}http://www.omdbapi.com/?apikey={apiKey}&s={this.query}&type=movie&page={page}`);
this.result = res.data;
this.result.Search.forEach(movies => moviesList.push(movies))
page++;
this.result = moviesList;
}
console.log(this.result);

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

`