Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys !! hope you are well ! im trying to to render some pokemon cards with data that i bring from an API (pokemonAPI). Im able to bring all the data that i need from the APi, exept for the type and abilities. But that is not the main probles , as i already bring the most important data that i needed. My problem comes inside the foreach that is un the renderPokemons function. Any clue of might i may solve this?

What I have tried:

const getAllPokemons =  async (url) => {
    try {
        const pokemons = await fetch(url);
        return pokemons.json()
    }catch (e) {
        console.log(e)
    }
}
const getSinglePokemon =  async (url) => {
    try {
        const pokemon = await fetch(url);
        return  pokemon.json()
    }catch (e) {
        console.log(e)
    }
}
document.addEventListener('DOMContentLoaded',async (ev)=> {
    const URL  = 'https://pokeapi.co/api/v2/pokemon';
    const pokemons  = await getAllPokemons(URL);
    const pomekonDataPromises = pokemons.results.map (async pokemon => {
        const pokeTemp = await getSinglePokemon(pokemon.url)
        return {
            name: pokemon.name,
            weight : pokeTemp.weight,
            height : pokeTemp.height,
            // type: pokeTemp.types.type.name,
            image : pokeTemp.sprites.other.dream_world.front_default,

        }
    })
    const pokemonJson = await Promise.all (pomekonDataPromises)
    console.log(pokemonJson)

    const parentContainer = document.querySelector('.containerCards');
    renderPokemons(parentContainer,pokemonJson);
})

////FUNCTION TO RENDER POKEMONS
const renderPokemons = (parent,apiResponse) => {
    apiResponse.forEach((data) => {
        const parentTemplate = document.createElement('div');
        parentTemplate.innerHTML=`<div class="card" style="width: 18rem;">
        <img class="img card-img-top" src="${data.sprites.other.dream_world.front_default}" alt="Pokemon image">
        <div class="card-body">
          <h5 class="name card-title">Name:</h5>
          <p class="weight card-text">Weight: </p>
          <p class="height card-text">Height: </p>
        </div>
      </div>`
      parent.appendChild(parentTemplate)
    });
}
Posted
Updated 10-Feb-21 10:22am

1 solution

const renderPokemons = (parent,apiResponse) => {
    apiResponse.forEach((data) => {
        const parentTemplate = document.createElement('div');
        parentTemplate.innerHTML=`<div class="card" style="width: 18rem;">
        <img class="img card-img-top" src="${data.image}" alt="Pokemon image">
        <div class="card-body">
          <h5 class="name card-title">Name: ${data.name}</h5>
          <p class="weight card-text">Weight: ${data.weight} </p>
          <p class="height card-text">Height: ${data.height}</p>
        </div>
      </div>`
      parent.appendChild(parentTemplate)
    });
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900