How to get the information from an api and display it on the DOM?

Viewed 147

I'm trying to get the stuff in my api to show up in the DOM. I keep getting an undefined but I know it's there. It shows up in my console. I just don't know what other extra things I need to do when the objects I want are nested into another array and how would I even get that onto the DOM.

document.addEventListener('DOMContentLoaded', () => {
  document.getElementById('songs').addEventListener('click', getSongs)

  //getSongs()
})

function getSongs() {
  let info = document.getElementById('info')
  let songList = document.getElementById('song-list')
  //info.innerHTML = ""
  songList.innerHTML = ""
    fetch('https://itunes.apple.com/search?term=K-Pop')
      .then(resp => resp.json())
      .then(data => {
        data.results.map(song => { 
          //console.log(song)
          songList.innerHTML += `
            <li>
             <a href="#" data-name="${song.trackName}"
             data-id="${song.trackId}"> ${song.trackName} </a>
            </li>
          `
        })
        attachLinks()
    })
}

function attachLinks() {
  const songs = document.querySelectorAll('li a')
  songs.forEach(song => {
    song.addEventListener('click', displaySong)
  })
}

function displaySong(event) {
  console.log(event.target)
  let songList = document.getElementById('song-list')
  let info = document.getElementById('info')
  /*songList.innerHTML = `<h1>Song Name:</h1>
                        <p>${event.dataset.trackName}</p>
                        <h2>Artist Name:</h2>
                        <p>${event.dataset.artistName}</p>
                        <h3>Release Date:</h3>
                        <p>${event.dataset.releaseDate}</p>`*/
  songList.innerHTML = ""
  info.innerHTML = ""
  fetch(`https://itunes.apple.com/search?term=K-Pop/${event.target.dataset.id}`)
      .then(resp => resp.json())
      .then(song  => {
        //console.log(song)
          info.innerHTML += `
          <h1>${song.trackName}</h1>
          <h3>Artist Name: </h3>
          <p>${song.artistName}</p>
          <h3>Release Date: </h3>
          <p>${song.releaseDate}</p>
        `
        })
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Music</title>
        <link rel="stylesheet" href="index.css"/>
    
    </head>
        <body>
            <h1>Music</h1>
            <h2 id='input'>K-Pop</h2>
            <button id="songs">All songs</button>
            <hr>
            <br>
        <div id="main">
            <ul id="song-list">

            </ul>
        <div id="info">

        </div>
        </div>
            <script src="index.js"></script>
        </body>
</html>

this is what all the data from the api looks like enter image description here

1 Answers

Seems like the second call is made to the wrong endpoint

Try changing this call

`https://itunes.apple.com/search?term=K-Pop/${event.target.dataset.id}`

For this

`https://itunes.apple.com/lookup?id=${event.target.dataset.id}`;

This should return some json in this form if we expect event.target.dataset.id to be '1574770128'

{
    "resultCount": 1,
    "results":
    [
        {
            "wrapperType": "track",
            "kind": "song",
            "artistId": 883131348,
            "collectionId": 1574770126,
            "trackId": 1574770128,
            "artistName": "BTS",
            "collectionName": "Permission to Dance - Single",
            "trackName": "Permission to Dance",
            "collectionCensoredName": "Permission to Dance - Single",
            "trackCensoredName": "Permission to Dance",
            "artistViewUrl": "https://music.apple.com/us/artist/bts/883131348?uo=4",
            "collectionViewUrl": "https://music.apple.com/us/album/permission-to-dance/1574770126?i=1574770128&uo=4",
            "trackViewUrl": "https://music.apple.com/us/album/permission-to-dance/1574770126?i=1574770128&uo=4",
            "previewUrl": "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/e1/6e/12/e16e1227-97a2-acf8-e3e0-6685f746695a/mzaf_12664711249777906736.plus.aac.p.m4a",
            "artworkUrl30": "https://is3-ssl.mzstatic.com/image/thumb/Music125/v4/cd/9b/1f/cd9b1fec-28b4-cbb9-42f5-adffcfbeba36/source/30x30bb.jpg",
            "artworkUrl60": "https://is3-ssl.mzstatic.com/image/thumb/Music125/v4/cd/9b/1f/cd9b1fec-28b4-cbb9-42f5-adffcfbeba36/source/60x60bb.jpg",
            "artworkUrl100": "https://is3-ssl.mzstatic.com/image/thumb/Music125/v4/cd/9b/1f/cd9b1fec-28b4-cbb9-42f5-adffcfbeba36/source/100x100bb.jpg",
            "collectionPrice": 0.69,
            "trackPrice": 0.69,
            "releaseDate": "2021-07-09T12:00:00Z",
            "collectionExplicitness": "notExplicit",
            "trackExplicitness": "notExplicit",
            "discCount": 1,
            "discNumber": 1,
            "trackCount": 1,
            "trackNumber": 1,
            "trackTimeMillis": 187585,
            "country": "USA",
            "currency": "USD",
            "primaryGenreName": "K-Pop",
            "isStreamable": false
        }
    ]
}

To map the response to the view you need to do something like this:

fetch(`https://itunes.apple.com/lookup?id=${event.target.dataset.id}`)
    .then(resp => resp.json())
    .then(data  => {
        const song = data.results[0];
        if (song) {        
            info.innerHTML += `
            <h1>${song.trackName}</h1>
            <h3>Artist Name: </h3>
            <p>${song.artistName}</p>
            <h3>Release Date: </h3>
            <p>${song.releaseDate}</p>
            `;
        }
    })
Related