0

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

2
  • 1
    i have upvoted your question to remove negative... when you ask qn on SO, it will be good to add relavent code snippets in your question (not just a sandbox link) Commented May 18, 2020 at 7:52
  • sure, thank you @gdh Commented May 18, 2020 at 7:58

1 Answer 1

3

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;
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

but this gives me a whole array, and not just one value, for example i need balue in console to be countryRegion: {russia, US..}
just map thru it and extract the countryRegion ... i have updated the answer..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.