Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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):

// 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 :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

The problem your are facing is that not all objects have images.

Please try this code:

const displayInfo = (sunny) => {
    const htmlString = sunny
        .map((sunny) => {
          const img = sunny.image ? sunny.image.medium : "https://picsum.photos/200/300"
            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=${img} />
            </li>
        `;
        })
        .join('');
    sunnyList.innerHTML = htmlString; };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!