batuk nath bansiwala
batuk nath bansiwala

Reputation: 11

Not able to subscribe

i have create a method getProducts but not able to subscribe the response and getting the below error.

Property 'subscribe' does not exist on type '() => Observable<any>'

service file

   public cartItemList :any = [];
   public productsList  = new BehaviorSubject<any>([])
   constructor() { }
   
   getProducts(){
      return this.productsList.asObservable;
   }

component.ts file

  public totalItem:Number = 0;
  constructor(private cartService:CartService){}
  ngOnInit():void{
    this.cartService.getProducts()
    .subscribe((res:any)=>{
      this.totalItem =res.length;})
  } 

Upvotes: 0

Views: 32

Answers (1)

Matheus Carvalho
Matheus Carvalho

Reputation: 535

'asObservable' is a method not a property, so it´s just to add () and it should work.

getProducts(){
 return this.productsList.asObservable;
}

should turn into:

getProducts(){
 return this.productsList.asObservable();
}

Upvotes: 1

Related Questions