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

126
Visualizações
Display images on the browser with map method using url's coming from an API

I'm trying to display two images on the browser based on url's I got from an API. So,

  1. I have fetched the data
  2. run a map method that will run all over the array that I got, and whenever it running into an image it inserts it into a empty div using innerHTML.

and this is what i got:
enter image description here

HTML:

   <body>
        <button id="new-deck">New Deck, Please!</button>
        <button id="draw-cards">Draw</button>
        <script src="index.js"></script>
        <div id="container-card"></div>
    </body>

JS:

    let deckId
let container = document.getElementById('container-card')
function handleClick() {
    fetch("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
        .then(res => res.json())
        .then(data => {
            console.log(data)
            deckId = data.deck_id
        })
}

document.getElementById("new-deck").addEventListener("click", handleClick)

document.getElementById("draw-cards").addEventListener("click", () => {
    fetch(`https://deckofcardsapi.com/api/deck/${deckId}/draw/?count=2`)
        .then(res => res.json())
        .then(data => data.cards.map(item =>container.innerHTML += `<img src=${item.image}/>`))      
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are two issues:

  1. you are not submitting deckId during image fetch api call.
  2. image src url must be enclosed into quotes, otherwise backslash is added to the end of the url:

    let deckId
let container = document.getElementById('container-card')
function handleClick() {
    fetch("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
        .then(res => res.json())
        .then(data => {
            console.log(data)
            deckId = data.deck_id
        })
}

document.getElementById("new-deck").addEventListener("click", handleClick)

document.getElementById("draw-cards").addEventListener("click", () => {
    fetch(`https://deckofcardsapi.com/api/deck/${deckId}/draw/?count=2`)
        .then(res => res.json())
        .then(data => data.cards.map(item =>container.innerHTML += `<img src="${item.image}"/>`))      
})
        <button id="new-deck">New Deck, Please!</button>
        <button id="draw-cards">Draw</button>
        <script src="index.js"></script>
        <div id="container-card"></div>

Every time you are dealing with API calls, make sure you check what data you receive before you minimalistic your code with one-liners. and use catch for promises to catch any errors that might occur.

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