angularrxjsangular9fork-joincombinelatest

Rxjs combineLatest does returning the observable with in the suscribe


I am trying to get Userid and serviceId after I need to get a call based on the userid,serviceId problem here is it's not returning the observable to the ts.

Service.ts

function getData():observable<any>{
   combineLatest([
      this.activeSellService.getActiveSellServiceId(),
      this.authService.getUserId(),
    ]).subscribe(([serviceId, userId]) => {
      if (serviceId&& userId) {
        const Url =
          'users/' +
          `${userId}` +
          '/services/' +
          `${serviceId}` +
          '?value=test
        return this.http.get<any>(this.endPoint.getUrl(encodeURI(Url)));
      }
    })
}

Component.ts:

 this.service.getData().subscribe(data=>{console.log(data));

Even its not print the data in the console because the service is not returning observable. kindly help me out with this problem. else can we go with different solutions in rxjs?


Solution

  • try this out

    function getData():observable<any>{
       return combineLatest(
          this.activeSellService.getActiveSellServiceId(),
          this.authService.getUserId(),
        ).pipe(mergeMap([serviceId, userId]) => {
          if (serviceId && userId) {
            const Url =
              'users/' +
              `${userId}` +
              '/services/' +
              `${serviceId}` +
              '?value=test
            return this.http.get<any>(this.endPoint.getUrl(encodeURI(Url)));
          }
        })
    }
    

    note on the arguments of combinelatest and there is not subscription in getData

    eg in stackblitz:

    https://stackblitz.com/edit/angular-rxjs-combinelatest-hjyfa6?file=src/app/app.component.ts