Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'filter' of null in angular 2?

I am getting this error Cannot read property 'filter' of null

when I apply filter in angular 2 .here is my code

http://plnkr.co/edit/K46jJsnmHiONuqIsnuzW?p=preview

import {Pipe} from 'angular2/core';

@Pipe({
  name: 'sortByName',
  pure: false,
})
export class SortByNamePipe {

  transform (value, [queryString]) {
    // console.log(value, queryString);
    return value.filter((student)=>new RegExp(queryString).test(student.name))
    // return value;
  }
}
like image 922
user944513 Avatar asked Feb 11 '16 10:02

user944513


1 Answers

It's because you have data as input that are loaded asynchronously using an HTTP request.

You need to check this before being able to apply the filter:

export class SortByNamePipe {
  transform (value, [queryString]) {
    if (value==null) {
      return null;
    }

    return value.filter((student)=>new RegExp(queryString).test(student.name))
    // return value;
  }
}
like image 181
Thierry Templier Avatar answered Oct 14 '22 17:10

Thierry Templier