[Vuejs]-How to get data with axios from all api pages?

4👍

What I would do is, first, use an async function called getPageOfResults:

async function getPageOfResults(page) {
    const response = await axios.get('/crm/customer/?page=' + page);
    return response.data.results; // .results.values()? I don't know
}

Then, inside another async function, you can have a loop that does what you want:

async function getAllResults() {
    const customers = [];
    let lastResultsLength = 100;
    let page = 1;
    while (lastResultsLength === 100) {
        const newResults = await getPageOfResults(page);
        page++;
        lastResultsLength = newResults.length;
        customers = customers.concat(newResults);
    }
    return customers;
}

So you’ve got a variable keeping track of what page your on, and you keep on getting new pages until you get a page with less than 100 results.

You’re adding all the results together with the concat function, and returning the entire list at the end.

👤TKoL

Leave a comment