Я создаю приложение для отслеживания в React с использованием API с открытым исходным кодом. Я использую Ax ios для HTTP-запросов. Когда я передаю объекты, полученные из функции GET в индексе. js, я получаю сообщение об ошибке:
TypeError: Невозможно прочитать свойство 'NewConfirmed' из неопределенного
Кто-нибудь знает, почему это может происходить на основе приведенного ниже кода и почему параметры возвращаются неопределенными? Я попытался изменить свою деструктуризацию параметров, чтобы отразить то, как ее структурирует API, но это не сработало.
index. js
import axios from 'axios';
const url = 'https://api.covid19api.com/summary';
export const fetchData = async () => {
try {
const { data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, } } } = await axios.get(url);
return { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, } ;
} catch (error) {
console.log(error);
}
}
Приложение. js
import React from 'react';
import {Cards, Chart, CountryPicker} from './components';
import styles from './App.module.css';
import {fetchData} from './api';
class App extends React.Component {
state ={
data: {},
}
async componentDidMount() {
const fetchedData = await fetchData();
this.setState({data: fetchedData});
}
render() {
const { data } = this.state;
return(
<div className={styles.container}>
<Cards data = {data} />
<CountryPicker />
<Chart />
</div>
);
}
}
export default App;
Cards.jsx
import React from 'react';
import {Card, CardContent, Typography, Grid} from '@material-ui/core';
import styles from './Cards.module.css';
const Cards = ({data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered } } }) => {
return(
<div className = {styles.container}>
<Grid container spacing={3} justify="center">
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>New Confirmed</Typography>
<Typography variant="h5">{NewConfirmed.value}</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of active cases of COVID-19.</Typography>
</CardContent>
</Grid>
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>Total Confirmed</Typography>
<Typography variant="h5">{TotalConfirmed.value}</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of active cases of COVID-19.</Typography>
</CardContent>
</Grid>
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>New Deaths</Typography>
<Typography variant="h5">{NewDeaths.value}</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of active cases of COVID-19.</Typography>
</CardContent>
</Grid>
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>Total Deaths</Typography>
<Typography variant="h5">REAL DATA</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of recoveries of COVID-19.</Typography>
</CardContent>
</Grid>
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>New Recovered</Typography>
<Typography variant="h5">REAL DATA</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of deaths of COVID-19.</Typography>
</CardContent>
</Grid>
<Grid item component={Card}>
<CardContent>
<Typography color="textSecondary" gutterBottom>Total Recovered</Typography>
<Typography variant="h5">REAL DATA</Typography>
<Typography color="textSecondary">REAL DATE</Typography>
<Typography variant="body2">Number of deaths of COVID-19.</Typography>
</CardContent>
</Grid>
</Grid>
</div>
)
}
export default Cards;