getUTCMonth() and getUTCDate draws an error when comparing certain dates

I have tried to create a method to format some dates in my datatable which are based on the results from a sql server of ours. I cannot see any reason why certain dates would draw an error and the errors seem to be from my if statements. For instance when checking the month and day of Thu Dec 31 2015 00:00:00 GMT-0500 (Eastern Standard Time) in my If statement it draws an error from what I can see. I have included my function below:

          dateFormat(date1, date2) {
                startDate = new Date(date1)
                endDate = new Date(date2)

                let TotalDays = Math.ceil((endDate - startDate) / (1000 * 3600 * 24));
                console.log('start')
                console.log(startDate)
                console.log(startDate.getUTCDate())
                console.log(startDate.getUTCMonth())
                console.log(endDate)
                console.log(endDate.getUTCDate())
                console.log(endDate.getUTCMonth())
                if (TotalDays == 364 || TotalDays == 365 || TotalDays == 366) {
                    if (startDate.getUTCMonth() == 0 && StartDate.getUTCDate() == 1) {
                        //this is January start date
                        console.log('complete')
                        return 'CY' + dayjs(endDate).format('YYYY')
                    } else if (startDate.getUTCMonth() == 6 && startDate.getUTCDate() == 1) {
                        //this is July start date
                        console.log('complete')
                        return 'FY' + dayjs(endDate).format('YYYY')
                    } else {
                        //not a true FY or CY so default to RY
                        console.log('complete')
                        return 'RY' + dayjs(endDate).format('YYYY')
                    }
                } else {
                    //this date is not on a full calendar year or fiscal year
                    console.log('complete')
                    return dayjs(startDate).format('MMM YYYY') + ' - ' + dayjs(endDate).format('MMM YYYY')
                }

            },

and my console log draws an error inside of one of my axios requests hence the promise error. However, I used console.log to see where the code is breaking as it loops through my data.

                axios({
                method: 'post',
                url: window.location.origin + '/api/list',
                data: [],
                headers: {
                    'Content-Type': 'multipart/form-data',
                }
            }).then(response => {

                this.Results = response.data.map((obj) => {
                    return { ...obj, formattedPeriodBegins: this.dateFormat(obj.date_begins, obj.date_ends) };
                })

            }).catch(error => {
                console.log(error.response.data.error);
            });

My console log shows the following output. Which traces my code to failing at the if statement before the next complete tag I use to output.

(index):5186 Mon Apr 01 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
(index):5187 1
(index):5188 3
(index):5189 Tue Jun 30 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
(index):5190 30
(index):5191 5
(index):5208 complete
(index):5185 start
(index):5186 Thu Jan 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)
(index):5187 1
(index):5188 0
(index):5189 Thu Dec 31 2015 00:00:00 GMT-0500 (Eastern Standard Time)
(index):5190 31
(index):5191 11
(index):5272 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
    at (index):5272:48

>Solution :

Your problem is likely because your not seeing the real reason for the error.

This is because

}).catch(error => {                
  console.log(error.response.data.error);
});

error is likely of type Error, the Error object will not have a response property, and then doing error.response.data will be reading undefined.

The fix here, is just log the error object in full.

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

A slight note, generally speaking catch (e) { console.log(e); } is something you shouldn’t be doing very often, as errors propagating is usually what you want. So just removing the .catch(e) { might even be better..

Leave a Reply