Here is my initial code I wrote to console log the data I want
const fetchPokemon2 = () =>
{
for (let i = 1; i <= 151; i++)
{
const url = `/${i}`;
fetch(url)
.then( res => res.json())
.then( data =>
{
console.log(data);
const pokemon = {}
if (data.types['1'])
{
pokemon['name'] = data.name;
pokemon['id'] = data.id;
pokemon['sprite'] = data.sprites['front_default'];
pokemon['type1'] = data.types["0"].type.name;
pokemon['type2'] = data.types["1"].type.name;
}
else
{
pokemon['name'] = data.name;
pokemon['id'] = data.id;
pokemon['sprite'] = data.sprites['front_default'];
pokemon['type1'] = data.types["0"].type.name;
}
console.log(pokemon);
})
}
}
fetchPokemon2();
It worked fine but this isnt the remended way so I'm trying to do this
const fetchPokemon = () =>
{
const promises = [];
for (let i = 1; i <= 151 ; i++)
{
const url = `/${i}`
promises.push(fetch(url).then(resp => resp.json()));
};
Promise.all(promises).then( results =>
{
const pokemon = results.map(data =>
( { name : data.name
, id : data.id
, sprite : data.sprites["front_default"]
, type : data.types["0"].type.name
, type2 : data.types["1"].type.name
}
));
console.log(pokemon);
});
};
fetchPokemon()
However this line of code
type2: data.types["1"].type.name
is giving the error Uncaught (in promise)
TypeError: Cannot read property 'type' of undefined
which is confusing me a lot because this type: data.types["0"].type.name is perfectly fine could anyone help me out with this?
Here is my initial code I wrote to console log the data I want
const fetchPokemon2 = () =>
{
for (let i = 1; i <= 151; i++)
{
const url = `https://pokeapi.co/api/v2/pokemon/${i}`;
fetch(url)
.then( res => res.json())
.then( data =>
{
console.log(data);
const pokemon = {}
if (data.types['1'])
{
pokemon['name'] = data.name;
pokemon['id'] = data.id;
pokemon['sprite'] = data.sprites['front_default'];
pokemon['type1'] = data.types["0"].type.name;
pokemon['type2'] = data.types["1"].type.name;
}
else
{
pokemon['name'] = data.name;
pokemon['id'] = data.id;
pokemon['sprite'] = data.sprites['front_default'];
pokemon['type1'] = data.types["0"].type.name;
}
console.log(pokemon);
})
}
}
fetchPokemon2();
It worked fine but this isnt the remended way so I'm trying to do this
const fetchPokemon = () =>
{
const promises = [];
for (let i = 1; i <= 151 ; i++)
{
const url = `https://pokeapi.co/api/v2/pokemon/${i}`
promises.push(fetch(url).then(resp => resp.json()));
};
Promise.all(promises).then( results =>
{
const pokemon = results.map(data =>
( { name : data.name
, id : data.id
, sprite : data.sprites["front_default"]
, type : data.types["0"].type.name
, type2 : data.types["1"].type.name
}
));
console.log(pokemon);
});
};
fetchPokemon()
However this line of code
type2: data.types["1"].type.name
is giving the error Uncaught (in promise)
TypeError: Cannot read property 'type' of undefined
which is confusing me a lot because this type: data.types["0"].type.name is perfectly fine could anyone help me out with this?
Share Improve this question edited Jul 24, 2021 at 2:26 Mister Jojo 22.6k6 gold badges25 silver badges44 bronze badges asked Jul 24, 2021 at 2:07 ZavianZavian 311 gold badge1 silver badge5 bronze badges 1-
“
data.types["0"].type.name
is perfectly fine” — So what?data.types["0"]
exists,data.types["1"]
does not. You’re never checking if either of them exist. – Sebastian Simon Commented Jul 24, 2021 at 2:16
2 Answers
Reset to default 1This happens when there is no value for the specified array or object index. That means data.types[1]
is probably undefined, hence the error Cannot read property 'type' of undefined for data.types[1].type...
In the second code sample, you aren't checking if data.types[1]
exists. You can always access the value safely by doing:
...
type2: data.types[1] && data.types[1].type && data.types[1].type.name
...
In the API response, some of the values have only 1 element in the types array. That could be the issue. You need to check if data exists or not before assigning them.
type2: data.types[1] && data.types[1].type && data.types[1].type.name