2

I am trying to write a function which filters rows based on couple of properties. When I run the code, the list is not getting displayed and there's an error stating "TypeError: Cannot read property 'name' of undefined ". I am trying to search on columns name and location. Here's my html code

<tbody ng-if="routerList.length">
<tr ng-repeat="router in routerList | orderBy: rule|filter : search | limitTo:5:5*(naviagtion.currentPage-1) ">
    <td><span>{{router.routerId}}</span></td>
    <td><span>{{router.name}}</span></td>
    <td><span>{{router.macAddr}}</span></td>
    <td><span>{{router.primaryIP}}</span></td>
    <td><span>{{router.status}}</span></td>
    <td><span>{{router.location}}</span></td> 

</tr>
</tbody>

In my controller, I have a function search

$scope.search = function(item){
    if($rootScope.naviagtion.searchText == undefined){
        return true;
    }else{
        if(item.router.name.indexOf($rootScope.naviagtion.searchText) != -1 || item.router.location.indexOf($rootScope.naviagtion.searchText) != -1 ){
            return true;
        }
    }
    return false;
}

Can someone tell me what's wrong?

5
  • you misspelled navigation naviagtion. Commented Oct 27, 2017 at 20:37
  • I know. The existing code has spelling naviagtion. It's not the problem Commented Oct 27, 2017 at 20:38
  • then the problem is probably at item.router. Can't read name cause .router is undefined? Commented Oct 27, 2017 at 20:41
  • It says cannot read property of name of undefined Commented Oct 27, 2017 at 20:56
  • Can you console.log(item)? Commented Oct 27, 2017 at 21:37

1 Answer 1

1

The error message "TypeError: Cannot read property 'name' of undefined" means that there is a variable that has the dot operator on it that isn't defined. Based on the code you posted it looks like the router is not defined.

router.name

A quick check in your if statement could resolve the issue.

if(item && item.router && ...) { }

If item.router is defined then it could be somewhere else in your code where you have some undefined object with a dot operator to a 'name' property.

someUndefinedObject.name

I usually use a debugger to make sure my object is in-fact defined when I see this error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.