I was fetching a api from coingecko and its showing me its not a function when I used a map function
I am trying to fetch a api and I used map method to sort it
12 Replies
I tried using the forEach method also I get the same error
Both
.map
and .forEach
are array methods and you won’t be getting an array from an API call. You’ll have to parse the response first.
But without seeing your code there’s not much more help we can be.
so usually the api are arrays of objects right? we use fetch method to fetch them then we parse the data to json format which is key valur pair then what shld we do?
Never assume the format of any API response as each platform does it slightly different. What does your code log out? Is it an array? An object?
object
There you go. Objects are not arrays. You need to find the info within the supplied object before looping through it
so if I use data.values() then I need to loop through the objects which I will use forEach method
I don’t know what the shape of the data is so maybe…?
I tried values but getting the same error after I get the object
Object.values(data)
is what you want
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
It’s a static method on the Object
object, not a method on all objectsNope still the same error
okay we fetch the api then we use then function
res can we anything right?