Home > Software design >  Javascript error TypeError: Cannot read properties of undefined
Javascript error TypeError: Cannot read properties of undefined

Time:07-12

I really hope that someone will be able to help me with an error I am getting. I have an endpoint where I am returning a nested json response. I am looping through the dataset and writing the values to the console and works great. When I get to the end it seems to error with this

TypeError: Cannot read properties of undefined (reading 'orderTotal')

This is my code below and any help would be really appreciated. I have searched high and low for an answer.

 function updateChart() {
        
        async function fetchData() {
            const url = '/admin/Dashboard/GetEvents';
            const response = await fetch(url);

            const datapoints = await response.json();
            console.log(datapoints);

            return datapoints;
        }

        fetchData().then(datapoints => {
        
            var orderTotal=0.0;
            
            for (let i = 0; i < datapoints.length; i  ) {
                for (let j = 0; j <= datapoints[i].length; j  ) {
                    for (let k = 0; k < datapoints[i].length; k  ) {
                        orderTotal =datapoints[j][k].orderTotal;

                    }


                }
                console.log(orderTotal);
                console.log(j);
                console.log(k);

            }
         

            myChart.update();

        }).catch(e => {
        console.log(e);
    });;


    }

enter image description here

CodePudding user response:

Looks like one of the entries of array datapoints is undefined. In this case datapoints[j][k] is undefined and it is leading to the above error. You can verify the log for datapoints to confirm

  • Related