Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda