How call all page from an api. With this api I can't change limit by page.

Hello, if someone could help me to solve this problem please.
Here is my code :


import {fetch} from 'wix-fetch';
import {getJSON} from 'wix-fetch';

export function Authentification() {
  
  
   return fetch("url.io/oauth2/token", {
  method: "POST",
   headers : {
 "Content-Type" : "application/x-www-form-urlencoded",
 "Authorization" : "Basic xxx"
   },
   body : 'grant_type=client_credentials'
   })

 .then(res => res.json())
 .then(data => {

   var Token = String(data.access_token)

   var Authentification = String("Bearer " + Token)

   return Authentification;

 });

}

export async function AllCountries() {

await Authentification().then(function(token) {

const apiUrl = "url.io/v2/countries"
const pageNumber = 1
var actualUrl = apiUrl + "?page=" + pageNumber
console.log("actualUrl : " + actualUrl)
var ArrhasNextPageData = []
var promises = []

var tokenStr = token
console.log("token2Backend :" + tokenStr)

fetch(actualUrl, {
  method : "GET",
  headers : {
 "Content-Type" : "application/x-www-form-urlencoded",
 "Authorization" : tokenStr
   },
   body : 'grant_type=client_credentials',
})

 .then(res => res.json())
 .then(countries => {
   console.log("Test AllCountries " + JSON.stringify(countries))

   var keycount = Object.keys(countries.data).length
   console.log("keycount : " + keycount)
   var Data = JSON.stringify(countries.data[0].name)
   console.log("DataCountriesName" + Data)
   var hasNextPageData = JSON.stringify(countries.pagination.hasNextPage)
   console.log("hasNextPageData " + hasNextPageData)

   ArrhasNextPageData.push(hasNextPageData)
   console.log("ArrhasNextPageData " + ArrhasNextPageData)
   
   var hasNextPageValue = ArrhasNextPageData.shift()
   console.log("hasNextPageValue : " + hasNextPageValue)

})

return AllCountries;

})
}

What’s the question?

I don’t know how I can call all page at the endpoint since “hasnextpage = true”

@watibynight2011 Why wouldn’t you run the same fetch but this time put pageNumber=2 in the end of the actualUrl ?

meaning loop over the page.
Put the pageNumber and the allCountries array in the global scope (and use let instead of const).

let pageNumber = 1;
let allCountries = [];
export function getAllCountries(){
let url = 'https://something.com'; 
let fullUrl = url + '?page=' + pageNumber;
//run the fetch using fullUrl. do what ever you need, save the results in a global variable  then in the end:
allCountries.push(countries);
allCountries = allCountries.flat();
if(hasNextPage){
pageNumber++;
return getAllCountries();
}
return allCountries;
}

Thank you for help ! I have try this code, but it’s not working.

@J. D. 

import {fetch} from 'wix-fetch';
import wixSiteBackend from 'wix-site-backend';
import {getJSON} from 'wix-fetch';
import {badRequest, created, forbidden, options, put, response, WixHttpFunctionResponse, use, WixHttpFunctionRequest, post, get, ok, notFound, serverError} from 'wix-http-functions';

export function Authentification() {
  
  
   return fetch("https://oauth2.elenasport.io/oauth2/token", {
  method: "POST",
   headers : {
 "Content-Type" : "application/x-www-form-urlencoded",
 "Authorization" : "Basic Mm43b2ViNG8wZXZzdTRmcGVuYjQ4ZTc1b286amVoYW1kbmsxdmtwNmoybzg0dHU3YzU1MzA4MXFrcmJjNnMzaWFzOWVwbm5pa2VuZmhq"
   },
   body : 'grant_type=client_credentials'
   })

 .then(res => res.json())
 .then(data => {

   var Token = String(data.access_token)


   var Authentification = String("Bearer " + Token)

   return Authentification;


 });



}


var pageNumber = 1;
var allCountries = [];
export async function AllCountries(){

await Authentification().then(function(token) {

let url = "https://football.elenasport.io/v2/countries/"; 
let fullUrl = url + "?page=" + pageNumber;
var tokenStr = token
console.log("token2Backend :" + tokenStr)

fetch(fullUrl, {
  method : "GET",
  headers : {
 "Content-Type" : "application/x-www-form-urlencoded",
 "Authorization" : tokenStr
   },
   body : 'grant_type=client_credentials',
})

 .then(res => res.json())
 .then(countries => {
   console.log("Test AllCountries " + JSON.stringify(countries))

var keycount = Object.keys(countries.data).length
console.log("keycount : " + keycount)
var Data = JSON.stringify(countries.data[0].name)
console.log("DataCountriesName" + Data)
var hasNextPage = countries.pagination.hasNextPage
console.log("hasNextPageData " + hasNextPage)

allCountries.push(countries);
allCountries = allCountries.flat();

if(hasNextPage){
pageNumber++;
console.log("hasNextTrueNumberPage ++" + pageNumber)
return AllCountries();
}
})
console.log("Data Final" + JSON.stringify(allCountries))
return allCountries;
})
}

@watibynight2011 Post the json you get. Maybe it’ll help.

@jonatandor35 Hello, it’s the same issue. The loop does not work

@watibynight2011

  1. Post the retrieved JSON, maybe it’ll help to understand.

  2. Follow the 3rd party’s documentation