Weird error when using $scope.data.filter


#1

I’m doing a simple search and filter on an array counting around 150 objects.

$scope.search = function(){
    $scope.airports = "";
    var filtered = airports.data.filter(function(el){
      var str = $scope.data.searchQuery.toLowerCase(),
        test = el.name.toLowerCase();
      return RegExp('^'+str).test(test);
    });
    $scope.airports = filtered;
  }

in about 40% of times I’m getting the following error:
undefined is not an object (evaluating ‘elm.nodeType’)

Error: undefined is not an object (evaluating 'elm.nodeType')
extractElementNode@http://localhost:8100/lib/ionic/js/ionic.bundle.js:35302:16
cancelChildAnimations@http://localhost:8100/lib/ionic/js/ionic.bundle.js:36337:38
leave@http://localhost:8100/lib/ionic/js/ionic.bundle.js:35835:32
ngRepeatAction@http://localhost:8100/lib/ionic/js/ionic.bundle.js:33165:27
$watchCollectionAction@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22809:21
$digest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22942:31
$apply@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23205:31
$$debounceViewValueCommit@http://localhost:8100/lib/ionic/js/ionic.bundle.js:31961:20
$setViewValue@http://localhost:8100/lib/ionic/js/ionic.bundle.js:31933:37
listener@http://localhost:8100/lib/ionic/js/ionic.bundle.js:28352:25
eventHandler@http://localhost:8100/lib/ionic/js/ionic.bundle.js:11713:25

can anyone point me what it could be?


#2

Same deal for me, I’m hitting this as well and can’t figure it out. It seems to happen randomly, and the stack-trace gives no clues.


#3

OK, I’m not sure if your error is the same as mine since I can’t see your HTML, but in my case I had an ng-repeat that was using track by index. Once I changed that to track by item.id everything was fine.


#4

You should take care of checking in the view that the variable (array in this case) is not in an inconsistent state. Because it seems that is something about events cancelled and the digest cicle. Maybe in that 40% of times you’re refreshing the view by two differents actions/event handlers? Anyways, if you’re updating $scope.airports in your view and also accessing it’s elements after a search() function, try checking for the existence of airports in your view, > because undefined is not an object :wink:

What i mean:

<div **ng-if="airports"** ng-repeat="airport in airports...">
</div>

Cheers