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
Perpetuity
Perpetuity14mo ago
I tried using the forEach method also I get the same error
13eck
13eck14mo ago
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.
Perpetuity
Perpetuity14mo ago
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum%2Cdogecoin&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&precision=full')
.then(data=>data.json())
.then((data) => {
console.log(data);
let tableData = ""
data.forEach((values) => {
tableData += ` <tr>
<td>${values.usd}</td>
<td>${values.description}</td>
<td>${values.price
}</td>
</tr>`
document.getElementById('tablebody').innerHTML=tableData;
}).catch(err =>{console.log(err);} )
})
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum%2Cdogecoin&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&precision=full')
.then(data=>data.json())
.then((data) => {
console.log(data);
let tableData = ""
data.forEach((values) => {
tableData += ` <tr>
<td>${values.usd}</td>
<td>${values.description}</td>
<td>${values.price
}</td>
</tr>`
document.getElementById('tablebody').innerHTML=tableData;
}).catch(err =>{console.log(err);} )
})
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?
13eck
13eck14mo ago
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?
Perpetuity
Perpetuity14mo ago
object
13eck
13eck14mo ago
There you go. Objects are not arrays. You need to find the info within the supplied object before looping through it
Perpetuity
Perpetuity14mo ago
so if I use data.values() then I need to loop through the objects which I will use forEach method
13eck
13eck14mo ago
I don’t know what the shape of the data is so maybe…?
Perpetuity
Perpetuity14mo ago
Perpetuity
Perpetuity14mo ago
I tried values but getting the same error after I get the object
13eck
13eck14mo ago
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 objects
Perpetuity
Perpetuity14mo ago
Nope still the same error okay we fetch the api then we use then function
.then(res=>res.json())
.then((res) => console.log(res))
.then(res=>res.json())
.then((res) => console.log(res))
res can we anything right?