Xena
Xena

Reputation: 377

how to retrieve data from Rapid API

I'm trying to retrieve list of songs from Rapid API. Here is my script:

function loadData() {
      fetch("https://deezerdevs-deezer.p.rapidapi.com/search?q=eminem", {
      "method": "GET",
      "headers": {
        "x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
        "x-rapidapi-key": "45ea72d0cbmsh5a68a7ddaa76fefp17c953jsn0a280e030350"
      }
    })
    .then(response => {
      response.json();
    })
    .then(
     parsedJson => {
          const list = document.querySelector('.list');
      parsedJson.forEach(element => {
        const listElem = document.createElement('li')
        const textNode = document.createTextNode(element.title)
        listElem.appendChild(textNode)
        list.appendChild(listElem)
      });
     }
    )
    .catch(err => {
      console.log(err);
    });

    }

I click on button and call function loadData() but it consoles an error :

TypeError: Cannot read property 'forEach' of undefined
    at D2.html:72

What I actually have to pass in that forEach.?

Upvotes: 1

Views: 965

Answers (1)

LuckyMario
LuckyMario

Reputation: 61

Problem is over here

.then(response => {
  response.json();
})

you have to return response.json(); so you can resolve it

.then(response => {
  return response.json();
})

Upvotes: 1

Related Questions