rawrstar
rawrstar

Reputation: 165

Not able to get values from the api to chart.js, react js

so this is my api, which is stored in a url "https://covid19.mathdro.id/api/confirmed"

this is my api index file

import axios from "axios";

const url = "https://covid19.mathdro.id/api/confirmed";

export const fetchData = async () => {

  try {
    const {
      data: countryRegion ,
    } = await axios.get(url);

    return {  countryRegion };
  } catch (error) {
    return error;
  }
};

in this sandbox code, i have tried to take the value of countryRegion from the api, but it appears as undefied in the console. https://codesandbox.io/s/react-chartjs-2-nqo9n

Upvotes: 0

Views: 88

Answers (1)

gdh
gdh

Reputation: 13702

Looks like you are destructuring incorrectly.

const { data: { countryRegion } } = await axios.get(changeableUrl);

Do this in your api

const { data: countryRegion } = await axios.get(changeableUrl);

Update based on the comment:

If only country is needed in the array, just map thru it and extract countryRegion

export const fetchData = async country => {
  try {
    let { data: countryRegion } = await axios.get(url);
    return { countryRegion: countryRegion.map(country => country.countryRegion) };
  } catch (error) {
    return error;
  }
};

Upvotes: 3

Related Questions