Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't get the images from an API to show up

I’m using an API to get information for a database sort of thing. I want the images to be displayed to the right of the text but the images aren’t showing up at all. I tried multiple different keys and still nothing. Here is what it currently looks like:
enter image description here

The images are not showing up as you can see.

Here is the JS (its pulling the data from here https://api.tvmaze.com/shows/347/episodes):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// DATABASE  const sunnyList = document.getElementById('sunnyList'); let sunnyInfo = [];

searchBar.addEventListener('keyup', (e) => {   const searchTarget = e.target.value.toLowerCase();   const filteredSunny = sunnyInfo.filter(sunny => {
    return sunny.name.toLowerCase().includes(searchTarget) || sunny.airdate.toLowerCase().includes(searchTarget) || sunny.airtime.includes(searchTarget)    });

  displayInfo(filteredSunny); });

const loadLayout = async () => {
    try {
        const res = await fetch('https://api.tvmaze.com/shows/347/episodes');
        sunnyInfo = await res.json();
        displayInfo(sunnyInfo);
    } catch (err) {
        console.error(err);
    } };

const displayInfo = (sunny) => {
    const htmlString = sunny
        .map((sunny) => {
            return `
            <li class="character">
              <div class="detail">
                <h2>${sunny.name}</h2>
                <p>Season ${sunny.season} Episode ${sunny.number}</p>
                <p>${sunny.airdate}</p>
                <p>${sunny.airtime}</p>
                <p>${sunny.rating.average}</p>
              </div>
                <img src="${sunny.image}"></img>
            </li>
        `;
        })
        .join('');
    sunnyList.innerHTML = htmlString; };

loadLayout();

I’ve tried sunny.image.medium and sunny.image.original but it still doesn’t show up.

Any help is appreciated 🙂

>Solution :

The image is not a url string, but an object with the following shape:

{
    medium: string,
    original: string
}

where both strings contain the actual image URLs.

For your use case medium probably makes more sense, so you can do this:

<img src="${sunny.image?.medium}"></img>

Edit

Added optional chaining because some items do not have image property.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading