Michael Letcher Michael Letcher - 3 months ago 7
AngularJS Question

AngularJS Cannot read property 'toLowerCase' of undefined occurs when adding an item to an array

When running a function that adds a new item to an array:

$scope.accounts.push(responce);


(the array that has a search filter, yes/no filter and is ordered.)

ng-repeat="x in accounts | searchFilter:searchString | enabledFilter:isEnabled | orderBy: ['-favourite','account'] as results"


Once a new item is added, this error occurs Cannot read property 'toLowerCase' of undefined when using the filters.

Note: After a refresh of the page, it will work again.

Answer

In your function, change the code to this:

if (responce !== undefined) $scope.accounts.push(responce);

This will prevent undefined values from being added to the array which should fix the error you were getting with the filter.