0

So, I have this simple axios script which is supposed to get bitcoin's image using the coingecko API.

const axios = require("axios");

async function test() {
    const { data } = await axios.get(
    `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin&order=market_cap_desc&per_page=100&page=1&sparkline=false`
    );
    console.log(data.image);
}

test();

Although, when I run it, it just returns undefined. Whenever I try to print data, it returns this: data

What am I doing wrong?

779804
  • 177
  • 1
  • 11
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json). You probably want `data[0].image` given your `data` appears to be an array – Phil Oct 29 '21 at 01:11
  • it worked, thanks – 779804 Oct 29 '21 at 01:49

1 Answers1

0

You are getting data as an array not an object Inside data it is at 0th index.you check inside data[0] and not inside data. So answer will be data[0].image

const axios = require("axios");

async function test() {
    const { data } = await axios.get(
    `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin&order=market_cap_desc&per_page=100&page=1&sparkline=false`
    );
    console.log(data[0].image);
}

test();
Phil
  • 157,677
  • 23
  • 242
  • 245
Vijay Palaskar
  • 526
  • 5
  • 15