简体   繁体   English

没有从 API 获取任何数据

[英]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在这里,我不想从这个 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但是每当我尝试调用 fetchDailyData 时,我只会在控制台上收到“DATA NOT FOUND”

您使用'而不是 `(靠近转义按钮下方)来使用字符串模板:

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

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.除了'错误的语法, data.map也会抛出一个错误: map is not a function ,因为你试图映射响应而不是 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) => ({

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM