Home > other >  how to return different type of observable
how to return different type of observable

Time:03-28

I have a observable method which is return some data I need to subscribe this from another observable method but I am getting error

public getSessionInfo(): Observable<UserInfo> {
   return this.getUserDetails().subscribe((data: any) => {
        return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })
    });
}

I am getting error that subscription is missing the following properties of type Observable of UserInfo, however the code section

return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })

is of same type which is Observable of UserInfo but I am not able to fix this. Please someone suggest the solution by noted down that I can't make any change in type of getSessionInfo methis as it is override method.

Thanks in advance.

CodePudding user response:

You are returning a Subscription instead of returning an Observable. Update the snippet including switchMap.

public getSessionInfo(): Observable<UserInfo> {
  return this.getUserDetails().pipe(
    switchMap((data: any) => of({
      userId: data.UserID,
      tenantId: data.tenantID,
      launchDarklySecureModeHash: '',
      roles: [''],
      emailId: data.EmailID,
      tenantName: data.name,
      userName: data.UserName,
      pendoJWTToken: '',
      location: '/',
      displayLocation: ''
    }))
  );
}
  • Related