0

So I'm trying to take an object from an API and push that into an array. I put it into a loop so that it will do it multiple times base on a number.

function arrayAPI(){
    console.log("arrayAPI ran");

    let dogPool = [];
    for (i=0; i<dogNumber; i++){
        fetch('https://dog.ceo/api/breeds/image/random')
        .then(response => responseJSON)
        .then(responseJSON => dogPool.push(responseJSON))
        .catch(error => alert('error! danger!'));
    }

    console.log(dogPool);

    createTable(dogPool);

However there is something wrong with my fetch code. It goes straight to the .catch. If I loop it, it will play the error message each time also.

The console.log(dogPool) tells me that the array is empty.

norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • See the referenced question for why `dogPool` is empty. For the rest: your code does not make sense. `response => respnseJSON`: what is `respnseJSON`? That is an undeclared variable? You should do `=> response.json()` to get the JSON from the response. See the examples in [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) documentation. – trincot Feb 09 '20 at 16:42
  • ``` let dogPool = []; for (i=0; i response.json()) .then(responseJSON => dogPool.push(responseJSON)) .catch(error => alert('error! danger!')); } ``` Ya, thanks. This works a lot better. no error. – Zackry Broodie-Stewart Feb 09 '20 at 17:01

0 Answers0