Search code examples
angularpublish-subscribeangular-httpclient

Angular HttpClient combine pipe, tap with subscribe?


I am trying to retrieve some data with HttpClient in Angular. My code looks like as follows:

getData(suffurl: string, id?:number): Observable<any[]> {
    return this.http.get<any[]>('localhost:5555/DNZ/'+ this.suff_url)
    .pipe(
      tap(data => console.log("Anlagenstatus Daten:", data)),
      catchError(this.handleError('getData',[])),
      subscribe(Response => { console.log(Response)})
    )
  }

However, I cannot use subscribe in the pipe method, or chain it before or after the .pipe method. The problem is, that without subscribe, it seems this code is not returning any data from the url or logging anything to the console although the link and the data exists?


Solution

  • You must subscribe to the method (since it returns an Observable), not inside the pipe.

    Try this instead

    getData(suffurl: string, id?:number): Observable<any[]> {
        return this.http.get<any[]>('localhost:5555/DNZ/'+ this.suff_url)
        .pipe(
          tap(data => console.log("Anlagenstatus Daten:", data)),
          catchError(this.handleError('getData',[])),
        )
      }
    

    then make a call

    this.getData("url").subscribe(Response => { console.log(Response)})