I've been struggling trying to understand how to access and and process data using HTTP method routes (get, put, post...). So far, I've been able to fetch the JSON data and store it in a global variable.
var pokedata;
fetch('https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json')
.then(function (res) {
return res.json();
}).then(function (json) {
pokedata = json;
}).catch(function () {
console.log("It was not possible to fetch the data.")
});
I wanted to send responses HTTP GETs to http://localhost:3000/pokemon/XXX/ with some data about that Pokémon number XXX (which is in the JSON called pokedata). However, any attempt at looping through the data inside GET, triggers the error:
app.get('/pokemon/:pokemonid', function (req, res) {
//not the desired behaviour but a sample of what doesn't work.
for (let {name: n, weight: w, height: h} of pokedata) {
res.send(n, w, h);
}
});
TypeError: pokedata[Symbol.iterator] is not a function
Can't seem to find anything related in the express docs. Any help is well received.
for .. ofloops don't work on objects, just iterables (with arrays being the most familiar iterable)fetch? If youconsole.logthepokedatain theapp.get, what does it look like?{ pokemon: [ { id: 1, num: '001', name: 'Bulbasaur', img: 'http://www.serebii.net/pokemongo/pokemon/001.png', type: [Object], height: '0.71 m', weight: '6.9 kg', candy: 'Bulbasaur Candy', candy_count: 25, egg: '2 km', spawn_chance: 0.69, avg_spawns: 69, spawn_time: '20:00', multipliers: [Object], weaknesses: [Object], next_evolution: [Object] }, { id: 2, num: '002', name: 'Ivysaur', ... ]}