0

I have the following filter which looks at two times, subtracts one from the other and if the figure is greater than 0 the filter passes

Here is the code

delayControllers.filter('customDelayFilter', function() {
  return function(data) {
    return data.filter(value => (new Date(value.etd) - new Date(value.std)) > 0);
  };
});

in google console errors are thrown

Cannot read property 'filter' of undefined

in phpstorm the => shows an error as does the > symbol (expression expected)

Now here is the rub, this code works exactly as expected when run. I call it using

 <tr ng-repeat="service in listOfServices | customDelayFilter">

I am only getting results where the row etd - std is greater than 0.. on ever device EXCEPT IOS. When i remove this filter the page works fine on IOS, so looks like Safari on IOS is being picky with the error

Is there a neater way to rewrite the above filter which was kindly supplied in a thread I raised some weeks ago (now closed)

1 Answer 1

2

Your filter expect a defined array. I suspect that listOfServices may be not yet initialised when the ng-repeat runs for the first time, depending how you initialise it.

Either, ensure listOfServices is initialised, or add a check in the filter, i.e.

delayControllers.filter('customDelayFilter', function() {
   return function(data) {
      if (data) {
         return data.filter(value => (new Date(value.etd) - new Date(value.std)) > 0);
      }
      else {
         return [];
      }
  };
});
Sign up to request clarification or add additional context in comments.

1 Comment

It should have pccured to me I was pulling the data asynchronously and the filter was operating on data that just wasnt there. I did try an ng-init="listOfServices=[]" but taht didnt work so I thought it was something else. Your solution worked perfectly. Thanks

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.