Skip to content

Instantly share code, notes, and snippets.

@brickbones
Created September 3, 2019 18:06
Show Gist options
  • Save brickbones/29a2c406387c15d4f1cc6ba5c18e45b9 to your computer and use it in GitHub Desktop.
Save brickbones/29a2c406387c15d4f1cc6ba5c18e45b9 to your computer and use it in GitHub Desktop.
Getting data from Pokeapi.co
const apiData = {
url: 'https://pokeapi.co/api/v2/',
type: 'pokemon',
id: '25',
}
const {url, type, id} = apiData
const apiUrl = `${url}${type}/${id}`
fetch(apiUrl)
.then( (data) => {
if(data.ok){
return data.json()
}
throw new Error('Response not ok.');
})
.then( pokemon => generateHtml(pokemon))
.catch( error => console.error('Error:', error))
const generateHtml = (data) => {
console.log(data)
const html = `
<div class="name">${data.name}</div>
<img src=${data.sprites.front_default}>
<div class="details">
<span>Height: ${data.height}</span>
<span>Weight: ${data.weight}</span>
</div>
`
const pokemonDiv = document.querySelector('.pokemon')
pokemonDiv.innerHTML = html
@ConnorDEllement
Copy link

Hello,

I am using visual studio, and have been following your YT tutorial on this, however I am getting an error.
Error: TypeError: Cannot set property 'innerHTML' of null
at generateHtml (pokemon.js:33)
at pokemon.js:18

I assure you, the code is typed as you have, and even tried copy and pasting your code directly from here, but it still does not work.
Can you help?

@brickbones
Copy link
Author

Can you share your code? @ConnorDEllement

@chadlatimer
Copy link

@brickbones I had the same problem. I copied your pokeapi-script.js code and got the following:

main_test.js:19 Error: TypeError: Cannot set property 'innerHTML' of null
at generateHtml (main_test.js:33)
at main_test.js:18

@MrGoldenBeard
Copy link

@brickbones I had the same problem. I copied your pokeapi-script.js code and got the following:

main_test.js:19 Error: TypeError: Cannot set property 'innerHTML' of null
at generateHtml (main_test.js:33)
at main_test.js:18

const apiData = {
url: 'https://pokeapi.co/api/v2/',
type: 'pokemon',
id: '25',
}

const {url, type, id} = apiData
const apiUrl = ${url}${type}/${id}

fetch(apiUrl)
.then( (data) => data.json())
.then( (pokemon) => generateHtml(pokemon) )

const generateHtml = (data) => {
console.log(data)
const html = <div class="name">${data.name}</div> <img src=${data.sprites.front_default}> <div class="details"> <span>Height: ${data.height}</span> <span>Weight: ${data.weight}</span> </div>
const pokemonDiv = document.querySelector('.pokemon')
pokemonDiv.innerHTML = html
}

@bharathjinka09
Copy link

bharathjinka09 commented Jun 30, 2020

Hi,
You need to add a html file with a class of pokemon. Then it will generate pikachu data inside the div.
Link this js file in html.

const apiData = {
url: 'https://pokeapi.co/api/v2/',
type: 'pokemon',
id: '25',
}

const {url, type, id} = apiData

const apiUrl = ${url}${type}/${id}

const generateHtml = (data) => {
console.log(data)
const html = <div class="name">${data.name}</div> <img src=${data.sprites.front_default}> <div class="details"> <span>Height: ${data.height}</span> <span>Weight: ${data.weight}</span> </div>
const pokemonDiv = document.querySelector('.pokemon');
console.log(pokemonDiv);
pokemonDiv.innerHTML = html;
}

fetch(apiUrl)
.then( (data) => {
if(data.ok){
return data.json()
}
throw new Error('Response not ok.');
})
.then( pokemon => generateHtml(pokemon))
.catch( error => console.error('Error:', error))

@bharathjinka09
Copy link

@fredianriko
Copy link

Hello, I've been following the tutorials and really like it, but i want to improve a little bit from how i would like to use the API,

from the tutorial, it shows pokemon depends on static id written inside the code, but i would like to make the pokemon change every couple second, my idea is to loop through the id object from the api, but i don't know how to code it, would anyone help me with this? Regards

@cNoNi
Copy link

cNoNi commented Jun 24, 2021

Hello, if some one want to copy this project + search field here you are

HTML

<title>Search Pokemons</title>
<script src="apiPoke.js"></script>
Enter No:

JavaScript

function srchPoke(){
const pokeID = document.getElementById("number").value;
//alert(pokeID)
const apiPokeData = {
url: 'https://pokeapi.co/api/v2/',
type: 'pokemon',
//id: 55,
}
const {url, type,} = apiPokeData
const apiUrl = ${url}${type}/
const finalApiUrl = apiUrl+pokeID

//console.log(pokeID)

fetch(finalApiUrl)
.then( (data) => {
if(data.ok){
return data.json()
}
throw new Error('Response not ok.');
})
.then( pokemon => generateHtml(pokemon))
.catch( error => console.error('Error:', error))

const generateHtml = (data) => {
console.log(data)
const html = <div class="name">${data.name}</div> <img src=${data.sprites.front_default}> <div class="details"> <span>Height: ${data.height}</span> <span>Weight: ${data.weight}</span> </div>
const pokemonDiv = document.querySelector('.pokemon')
pokemonDiv.innerHTML = html
}

@Bria222
Copy link

Bria222 commented May 17, 2022

hi nice job
but the html variable will bring up an error its rather nice if you use a different variable name and also the closing curry bracket is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment