0
Follow
0
View

Getting Error: Cannot read properties of undefined (reading 'map') chartjs

dujunting80 注册会员
2023-01-25 22:49

Your data is asynchronous. On page load, data is undefined until your data arrives. Add interrogation point like this, which means to make a map if the data exists

    useEffect(() => {
                const fetchAPI = async() => {
                        const resp = await httpClient.get("//localhost:5000/dashboard")
                        const data = resp.data
                        // console.log("From fetchapi", Object.values(data).map((crypto) => crypto.current))
                        if(data){
                        const keys_list = Object.keys(data).map((crypto) => crypto)
                        const data_list = Object.values(data).map((crypto) => crypto.current)

                        //console.log(data_list)

                        setChartData({
                                labels: keys_list,
                                datasets: [
                                        {
                                                label: 'Price in KRW',
                                                data: data_list,
                                        
                                                backgroundColor: [
                                                        "#ffbb11",
                                                        "#ecf0f1",
                                                        "#50AF95"
                                                ]
                                        }
                                ]
                        });

                 }

                        
                };
                fetchAPI()
        }, []);