Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Natty's avatar
Level 1

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

I need your help with this error I am facing colleagues. I am new to vue so I am finding it quite difficult to solve the error though I what what exactly is causing the error. I am creating a datatable in vue and I am trying to achieve data sorting with this tutorial I am following but end up getting the following error: TypeError: Cannot read properties of undefined (reading 'type')

Kindly refer to this code:

 computed: {
        filteredAccommodations(){
            let accommodations = this.accommodations;
            if (this.search) {
                accommodations = accommodations.filter((row) => {
                    return Object.keys(row).some((key) => {
                        return String(row[key]).toLowerCase().indexOf(this.search.toLowerCase()) > -1;
                    })
                });
            }

            let sortKey = this.sortKey;

            let order = this.sortOrders[sortKey] || 1;
            if(sortKey){
                accommodations = accommodations.slice().sort((a, b) => {
                    let index = this.getIndex(this.columns, 'name', sortKey);
                    a = String(a[sortKey]).toLowerCase();
                    b = String(b[sortKey]).toLowerCase();
                    
                    if (this.columns[index].type && this.columns[index].type === 'date') {
                        return (a === b ? 0 : new Date(a).getTime() > new Date(b).getTime() ? 1 : -1) * order;
                    } else if (this.columns[index].type && this.columns[index].type === 'number') {
                        return (+a === +b ? 0 : +a > +b ? 1 : -1) * order;
                    } else {
                        return (a === b ? 0 : a > b ? 1 : -1) * order;
                    }
                });
            }
            return accommodations;
            
        },

        paginatedAccommodations(){
            return this.paginate(this.filteredAccommodations, this.length, this.pagination.currentPage); 
        }
    },

Counting on your help. Thank you.

0 likes
10 replies
undeportedmexican's avatar

I believe your issue comes on this part of your code: this.columns[index].type && this.columns[index].type === 'date'

What the error is sayings is "I'm trying to access the 'type' property in an undefined object. Which means, the underlying object doesn't exist.

Is it possible that this.columns doesn't have any content at all?

Natty's avatar
Level 1

@undeportedmexican Thank you for your reply. I don't seem to get you but if you are trying to ask whether "the columns" is getting data, then it is getting data please. And the error is definitely coming from the area you pointed to.

undeportedmexican's avatar

@Natty

I struggled at it first. Let's take a look at the error:

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

Basically, the error means, "I am trying to read the property 'type' of an undefined object'.

In your case, you have an array of objects, of which you're trying to read the property type:

this.columns[index].type && this.columns[index].type === 'date'

What is surely happening is that you're trying to access an index that's not defined in the array (eg, trying this.columns[3] in an array with only 3 elements)

So what you need to debug is whether index actually exists in your array. Try console logging index as soon as you calculate it, it'll probably point you in the right direction.

undeportedmexican's avatar

@natty

What does getIndex() do? Can you share that method?

let index = this.getIndex(this.columns, 'name', sortKey);
devmenezes's avatar

Please console.log the following three expressions( separately)

this

this.columns

this.columns[index]

Make this after the following line String(b[sortKey]).toLowerCase()

Let us know the result

Natty's avatar
Level 1

@devmenezes, Thanks for your response. Please I am trying to use a different approach as this particular approach stressed me. Thank you.

Bantaker's avatar

@Natty Your persistence is commendable, but if you do not succeed, then listen to the advice above.

devmenezes's avatar

This is not meant to be the solution. Just a debbuging task.

Please or to participate in this conversation.