Wira Xie
Wira Xie

Reputation: 839

Angular, TypeError: Cannot read property 'toLowerCase' of undefined

i'm trying to make a custom filter pipe by following this link, but i got error that said Angular, TypeError: Cannot read property 'toLowerCase' of undefined. I already imported the pipe to app.module.ts and also declared it in declaration. Can anyone help me with this error?

<form id="filter">
  <input type="text" class="form-control" name="term" [(ngModel)]="term" placeholder="filter by name" />
</form>

<tr *ngFor="let product of productList | paginate: { itemsPerPage: 1, currentPage: p } | filter: term">
  <td>{{product.prdName}}</td>
  <td>{{product.prdCat}}</td>
  <td>{{product.prdSup}}</td>
</tr>

@Pipe({
  name: 'filter'
})


transform(prdName: any, term: any): any {
    if (term === undefined) return prdName;

    return prdName.filter(function(Product) {
      return Product.name.toLowerCase().includes(term.toLowerCase());
    })

Upvotes: 3

Views: 24560

Answers (2)

Chandru
Chandru

Reputation: 11192

try like this :

transform(items: any, term: any): any {
    if (term === undefined) return items;

    return items.filter(function(Product) {
        return Product.prdName.toLowerCase().includes(term.toLowerCase());
    })
}

Upvotes: 4

Christian Benseler
Christian Benseler

Reputation: 8075

Is the attribute called 'name' or 'prdName' (the one you used in the template is 'prdName')?

transform(values, args) {
    values.filter(
      product => product.prdName.toLowerCase().includes(args.toLowerCase())
    )
}

Upvotes: 1

Related Questions