Created
September 3, 2019 18:06
-
-
Save brickbones/29a2c406387c15d4f1cc6ba5c18e45b9 to your computer and use it in GitHub Desktop.
Getting data from Pokeapi.co
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Hello, if some one want to copy this project + search field here you are
HTML
<title>Search Pokemons</title> <script src="apiPoke.js"></script>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
}