Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda