[Solved] Typeerror: can read property ‘index of’ of null

Cannot read property ‘indexOf’ of null

 

 let result = this.pages1.values.filter(row => {//this.pages1.values this is all the data in the table
              // search the content of that column, here search the content of the name, id and other columns
              return row.name.indexOf(this.search1) > -1 
                || row.id.indexOf(this.search1) > -1 
                || row.code.indexOf(this.search1) > -1 
                || row.note.indexOf(this.search1) > -1;
            });

error message:

TypeError: Cannot read property 'indexOf' of null
    at app.43208ea5.js:1
    at Array.filter (<anonymous>)
    at o.sousuo (app.43208ea5.js:1)
    at click (app.43208ea5.js:1)
    at it (chunk-vendors.ffbff91e.js:34)
    at o.n (chunk-vendors.ffbff91e.js:34)
    at it (chunk-vendors.ffbff91e.js:34)
    at o.In.e.$emit (chunk-vendors.ffbff91e.js:34)
    at o.handleClick (chunk-vendors.ffbff91e.js:1)
    at it (chunk-vendors.ffbff91e.js:34)

means the indexOf when used to determine the front row. Name the error, all you need to use to determine whether these is empty before indexOf

if(row.name! =null||row.id! = null | |…). This is too much trouble

We can write a method:

contains(prop, str) { 
            if (prop === null || prop === undefined) {
                return false;
            } else {
                return prop.indexOf(str) > -1;
            }
        },

and then modify the wording, I here the VUE method and invoke the contains is to add this, if it is js will remove the this

 let result = this.pages1.values.filter(row => {
               return this.contains(row.name, this.search1) 
                || this.contains(row.id, this.search1) 
                || this.contains(row.code, this.search1) 
                || this.contains(row.note, this.search1) 
            });

Similar Posts: