Chart.jsx TypeError: Cannot read property 'length' of undefined COVID-19 Tracker Application

Viewed 64

DESCRIPTION Hello, can someone help me find this error I don't understand it. This code comes from a youtube video that tells you how to do a covid tracker. Here I try ad do the chart part although it doesn't let me use the ".length" see error below. Can someone help me please?

HERE IS HOW I STRUCTURED MY FILES FILE STRUCTURE

HERE is MY ERROR This is the error given to me when I try and run the code

CHART.JS file

import React, { useState, useEffect } from 'react';
import { fetchDailyData } from '../../api';
import { Line, Bar } from 'react-chartjs-2';

import styles from './Chart.module.css';

const Chart  = () => {
    const   [dailyData,setDailyData ] = useState([]);

    useEffect(()=>{
        const fetchAPI =  async () => {
          setDailyData(await fetchDailyData());
        }
 
        fetchAPI();
 
    },[]);
  

    const lineChart = (
        dailyData.length
        ? (
            <Line
            data = {{
                labels: dailyData.map(({ date }) => date),
                datasets: [{
                    data: dailyData.map(({ confirmed }) => confirmed),
                    label: 'Infected',
                    borderColor: '#3333ff',
                    fill: true,
                }, {
                    data: dailyData.map(({ deaths }) => deaths),
                    label: 'Deaths',
                    borderColor: 'red',
                    backgroundColor: 'rgba(255, 0, 0, 0.5)',
                    fill: true,
                }],

            }}
            />) : null
    );

    return (
        <div className = {styles.container}>
            {lineChart}
        </div>
    )
}

export default Chart; 

Index.js file (of api)

import axios from 'axio[![enter image description here][2]][2]s';

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');

        const modifiedData = data.map((dailyData) => ({
            confirmed: dailyData.confirmed.total,
            deaths: dailyData.deaths.total,
            date: dailyData.reportDate,
        }));

    return modifiedData;
    }catch(error){

    }
}
1 Answers

EDIT:

I had some time to look over your api.js.

If you haven't looked at my repo or found any solution yet, you should look and learn how to use a get request to a server because you are not doing it right.

I am not going to fix your code and write it all again. But I did attach some sources for you to learn how to get a request from a server.

Learn about JS Promise here Learn about JS Fetch here

I really recommend you to learn the above things before you are trying to work with Axios or any other library to send/post a request.

Good luck,

Related