最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript: Uncaught TypeError: Cannot read property 'indexOf' of null - Stack Overflow

programmeradmin1浏览0评论

I'm populating a table with data - using fixed-data-table, which is a React.js ponent. However, that isn't so important at this stage.

The table has a search box where the issue stems from.

First, here's the interesting part of the code.

for (var index = 0; index < size; index++) {
            if (!filterBy || filterBy == undefined) {
                filteredIndexes.push(index);
            }
            else {

                var backendInfo = this._dataList[index];

                var userListMap = hostInfo.userList;

                var userListArr = Object.values(userListMap);


                function checkUsers(){
                    for (var key in userListArr) {
                        if (userListArr.hasOwnProperty(key) && userListArr[key].text.toLowerCase().indexOf(filterBy) !== -1) {
                            return true;
                        }
                    }
                    return false;
                }


                if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 || backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
                    || backendInfo.userListMap.indexOf(filterBy) !== -1) {
                    filteredIndexes.push(index);
                }

            }
        }

This is rendered and the last part is throwing errors if you input something in the table, and a column returns null in the given cell.

The thing is, I can make the code work if I change the last part to ..

        try {
            if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 ||    backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
            || backendInfo.userListMap.indexOf(filterBy) !== -1) {
            filteredIndexes.push(index);
            }
        }
        catch(err) {
            console.log('Exception')
        }

With the try/catch, it works 100% as intended and handles the indexOf returning null... But this can't be the way to properly handle it - I'm assuming this sort of exception handling is, well, supposed to be for rare exceptions, and shouldn't really be used on the front-end as much as the backend.

How do I handle the error in the title without using try/catch?

I'm populating a table with data - using fixed-data-table, which is a React.js ponent. However, that isn't so important at this stage.

The table has a search box where the issue stems from.

First, here's the interesting part of the code.

for (var index = 0; index < size; index++) {
            if (!filterBy || filterBy == undefined) {
                filteredIndexes.push(index);
            }
            else {

                var backendInfo = this._dataList[index];

                var userListMap = hostInfo.userList;

                var userListArr = Object.values(userListMap);


                function checkUsers(){
                    for (var key in userListArr) {
                        if (userListArr.hasOwnProperty(key) && userListArr[key].text.toLowerCase().indexOf(filterBy) !== -1) {
                            return true;
                        }
                    }
                    return false;
                }


                if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 || backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
                    || backendInfo.userListMap.indexOf(filterBy) !== -1) {
                    filteredIndexes.push(index);
                }

            }
        }

This is rendered and the last part is throwing errors if you input something in the table, and a column returns null in the given cell.

The thing is, I can make the code work if I change the last part to ..

        try {
            if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 ||    backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
            || backendInfo.userListMap.indexOf(filterBy) !== -1) {
            filteredIndexes.push(index);
            }
        }
        catch(err) {
            console.log('Exception')
        }

With the try/catch, it works 100% as intended and handles the indexOf returning null... But this can't be the way to properly handle it - I'm assuming this sort of exception handling is, well, supposed to be for rare exceptions, and shouldn't really be used on the front-end as much as the backend.

How do I handle the error in the title without using try/catch?

Share asked Feb 24, 2017 at 10:20 cbllcbll 7,23929 gold badges79 silver badges124 bronze badges 1
  • provide values for variables – Sagar V Commented Feb 24, 2017 at 10:24
Add a ment  | 

1 Answer 1

Reset to default 3

You are using indexOf, so make sure the values will not be undefined or null, You can solve it by putting the check on each values like this:

let {firstName, lastName, countryOrigin, userListMap} = backendInfo;

if ((firstName && firstName.indexOf(filterBy) !== -1) 
      || (lastName && lastName.toLowerCase().indexOf(filterBy) !== -1) 
      || (countryOrigin && countryOrigin.toLowerCase().indexOf(filterBy) !== -1)
      || (userListMap && userListMap.indexOf(filterBy) !== -1)) {
            filteredIndexes.push(index);
}

or other way of solving this is, the default values you defined of these variables firstName, lastName, countryOrigin, userListMap, if they are array then it should be [], and if string then it should be ''.

发布评论

评论列表(0)

  1. 暂无评论