1
votes

I have tried using custom pipe.This is my pipe file

import { Pipe, PipeTransform } from '@angular/core';
import { User } from 'src/app/user';
import { ApiService } from 'src/app/api.service';
import { Observable } from 'rxjs';
import { isNgTemplate } from '@angular/compiler';
import { map } from 'rxjs/operators';

@Pipe({
  name: 'my-pipe'

})
export class MyPipePipe implements PipeTransform {
  isEqual = false;
  api: ApiService;

  transform(post_id: number, users: Observable< User[]> ): any {
   // users = this.api.getUserList();
    users.subscribe(user => user.filter(userss => {
      if (userss.id === post_id && post_id !== null) { this.isEqual = true; } else {
        console.log(this.isEqual + ' ' + ' hataa');
      }
    }));

  }

All i want is nonexistent users cant post.Therefore i trying user's id equalize with post's userId.Here is my html code

    <div class="form-group">
      <label for="userId">User Id</label>
      <input type="number" class="form-control" id ="userId" name="userId" required [(ngModel)]="posts.userId">
     <!--<div   class="tetx text-danger" *ngIf="!isEqual" >Invalid Id!!!</div>--> 
      <div  *ngIf=" my-pipe: posts.userId " >

Invalidd


      </div>

    </div>

But unfortunately I get this error:

[Angular] TypeError: Cannot read property 'toUpperCase' of undefined

1
Your code has no signs of it having toUpperCase in there. Where exactly are you using it? - SiddAjmera
<div *ngFor="let u of users_s | my-pipe " > i got error in this line - cmyldz
Please poste your full stack trace. As text, not as image. - user4676340
@trichetriche i posted like this with intent to being more understandable - cmyldz
@trichetriche but if necessary; i can poste all my code - cmyldz

1 Answers

1
votes

The error comes from Angular compiler that tries to compile your template.

Choose another name for your pipe, i.e. myPipe:

@Pipe({
  name: 'myPipe'

...

*ngFor="let u of users_s | myPipe"

And you can't use pipe without passing value to it:

*ngIf=" my-pipe: posts.userId "

it should be something:

*ngIf="postId | myPipe"