Home > OS >  How to compose url from rxjs `BehaviorSubject`
How to compose url from rxjs `BehaviorSubject`

Time:11-15

this is the BehaviorSubject getting value:

private searchClinicByParams$: BehaviorSubject<SearchClinicByParamsProps> = new BehaviorSubject(initialSearchParams);

here is my method needs to compose the vaues:

getParams() {
        const values = this.searchClinicByParams$.pipe(
            map((data) => data)
        );
        console.log('values', values);
    }

at present above map not returns any value.

here is my get method:

 clinicList$ = this.http
        .get<AddClinicProps[] | null>(
            this.URL  
                `hfs-admin/customer-application/clinics?name=OUS&adminUserName=${this.getParams()}`
        )
        .pipe(
            map((clinics) => clinics),
            catchError(this.handleError)
        );

how can i get return value at this.getParams() or what is the correct way to do it?

CodePudding user response:

Your getParams() function has a misleading name, it doesn't get anything since you are not returning anything, I always suggest explicitly setting the return type for your function, in this case, it should be an Observable of type SearchClinicByParamsProps given your example:

getParams(): Observable<SearchClinicByParamsProps> {
  return this.searchClinicByParams$.asObservable();
}

And then, in your function where you are making the HTTP call:

clinicList$ = this.getParams().pipe(
  mergeMap((params) => this.http.get(`${this.url}hfs-admin/customer-application/clinics?name=OUS&adminUserName=${params}}`)),
  catchError(this.handleError)
) 

I assume somewhere in your application, something is listening to clinicList$, so when it subscribes, you will get your response.

CodePudding user response:

There're 2 things not being done right logically.

  1. getParams() is not returning anything.
  2. You are using getParams() as if it is returning a string

Despite the naming of your functions, you may either

  1. Subscribe the value from searchClinicByParams$ to a local variable and use it in your HTTP request.
  2. Return the observable in getParams() and pipe it to your HTTP request with one of the mapping operators.
  • Related