To test my understanding of the map() I "fetch"'ed data from the Starwars api url: 'https://swapi.co/api/people' and assigned it to the const 'data'
Since I was only interested in mapping the characters found in results[] I created a const char pointing to that array ie. const char = data.results. When I tried to map the array I got the error "cannot read property of 'map' undefined. I carried out some confidence checks (console.logs) to confirm that the array char existed before attempting to perform the mapping function on it. It does ...so I'm not sure why the following doesn't work char.map((ele) => )
I also tried the mapping with a what I believe to be an exact duplicate of the results array and the mapping worked fine. So clearly there is something I am missing. Any help would be greatly appreciated
// Get data from Web based on Selection const [ data, isloading ] = useFetch( 'https://swapi.co/api/people');
// test to see if we received from useFetch
console.log('Here is the loaded Data', data);
// Just want to map the characters in the Results array so point chars to the results array in data
const chars = data.results;
console.log(`Check that chars and data.results are equal", ${data.results === chars}`)
// Now Show the Characters
console.log('Here are the Characters from Website after pointing to data.results', chars);
console.log('Here are the Characters using the static array', charsStatic)
const characterList = chars.map((ele) =>
<Card key={ele.name} name={ele.name} height={ele.height} hair={ele.hair_color} skin={ele.skin_color} vehicle={ele.vehicles} />)
return (
<div>
<h1>List of Starwar Characters</h1>
<div className='container'>
{isloading ? "Waiting for Data" : characterList}
</div>
</div>
);
}
I expected to be able to map the chars array since I could clearly see it when I logged the chars to console. Instead I received the "cannot read property map of undefined" and yet, to me, the char array is defined ;(