Ritik Kumar
Ritik Kumar

Reputation: 701

Not getting any data from API

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

Upvotes: 0

Views: 158

Answers (2)

Luis Paulo Pinto
Luis Paulo Pinto

Reputation: 6036

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) => ({

Upvotes: 1

Omri Attiya
Omri Attiya

Reputation: 4037

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

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

Upvotes: 2

Related Questions