Issue
I am learning RxJS. I have 3 api call's, I need to make 2nd api call, and pass it's data as a parameter to 3rd api call. I tried this:
    checkPermission(permissionName: string): Observable<boolean> {
    this.check(this.p1)
      .pipe(
        switchMap(res => {
          const shouldCheck = res.Value;
          if (shouldCheck.toLowerCase() === 'true') {
            return this.checkPermission(permissionName).pipe(
              map(result => {
               
                return result;
              })
            );
          } else return of(true);
        })
      )
      .subscribe(permission => {
       
      });       
    }
But getting syntax error.
Solution
The code you posted is not very comprehensible, but I'll try to make something usable out of it:
  checkPermission(permissionName: string): Observable<boolean> {
    return this.checkSetting(this.p1).pipe(
      map((res) => res.SysConfig.Value.toLowerCase() === 'true'),
      switchMap((shouldCheck) =>
        iif(
          () => shouldCheck,
          this.permission(this.p2).pipe(
            switchMap((data) =>
              this.hasPermission(permissionName, data.SysConfig.Value)
            ),
            // hoping that res.permission is a boolean
            map((res) => res.permission)
          ),
          of(true)
        )
      )
    );
  }
You should also omit the subscribe if you plan on returning Observable<boolean>. You can subscribe to the observable returned by this method in the place where you call it:
this.authService.checkPermission(permission).subscribe(hasPermission => {
  console.log(`User ${hasPermission ? 'has' : 'does not have'} ${permission} permission`);
});
Answered By - Octavian Mărculescu
 
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.