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.

2
  • 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 documentation. Commented Feb 9, 2020 at 16:42
  • ``` let dogPool = []; for (i=0; i<dogNumber; i++){ fetch('dog.ceo/api/breeds/image/random') .then(response => response.json()) .then(responseJSON => dogPool.push(responseJSON)) .catch(error => alert('error! danger!')); } ``` Ya, thanks. This works a lot better. no error. Commented Feb 9, 2020 at 17:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.