0
import axios from 'axios';

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

export const fetchData = async () => {
  try {
    const { data: { confirmed, recovered, deaths, lastUpdate} } = await axios.get(url);
    return {confirmed, recovered, deaths, lastUpdate};
} catch (error) {
}
}
export const fetchDailyData = async()=> {
try{
    const data = await axios.get('${url}/daily');
    console.log("DATA",data);
    const modifiedData = data.map((dailyData) => ({
        confirmed: dailyData.confirmed.total,
        deaths: dailyData.deaths.total,
        date: dailyData.reportDate,
    }));
    return modifiedData;
} catch(error){
    console.log("DATA NOT FOUND");
    var r = []
    return r
}
}

Here I'mt trying to get data from this API: https://covid19.mathdro.id/api/daily

But Whenever I'm trying to call fetchDailyData , I'm only getting "DATA NOT FOUND" on the console

4
  • Please write console.log("DATA NOT FOUND", error) and paste the result in your answer. The endpoint you are calling supports cross origin requests so your code should work. The error may be in the data.map Commented Jun 24, 2021 at 18:05
  • No the error is in fetching the data from the API endpoint because the console log written below the axios.get is also not printing. Commented Jun 24, 2021 at 18:08
  • Data not found Error: Request failed with status code 404, this is the error I'm getting Commented Jun 24, 2021 at 18:12
  • Take a look at the answer, this is your problem Commented Jun 24, 2021 at 18:13

2 Answers 2

2

You used ' instead of ` (near below escape button) to use string templates:

const data = await axios.get(`${url}/daily`);
Sign up to request clarification or add additional context in comments.

Comments

1

Besides the ' wrong syntax, the data.map will also throw an error: map is not a function, because you are trying to map the response and not the response.data.

You should do something like that:

const data = await axios.get('${url}/daily');

To:

const response = await axios.get(`${url}/daily`);

And your map:

const modifiedData = data.map((dailyData) => ({

To:

const modifiedData = response.data.map((dailyData) => ({

Comments

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.