Home > OS >  Angular: wait for Observable inside a function before returning result
Angular: wait for Observable inside a function before returning result

Time:08-12

I need help with the following function:

  getUser(uuid: string): Observable<WowUserDataModel> {
    let user: WowUserDataModel = {
      login: null,
      userUuid: uuid,
      firstName: null,
      lastName: null,
      displayName: null,
      email: null,
      authorities: null
    };

    return this.http.get(WowUrlProvider.gateway.wowUserAuthorities.replace('{userUuid}', uuid.toUpperCase())).pipe(
      map((authorities) =>
        user.authorities = authorities,
      )
    );
  }

What I need: to wait for the this.http.api call (which returns Observable) to complete, assign the result to user.authorities, and only then return Observable of the entire user object (always a single user)

What is happening now: the function returns just authorities property, not the whole user object

Can this be achieved without changing the return type of the function?

CodePudding user response:

getUser(uuid: string): Observable<WowUserDataModel> {
  // ...
    return this.http.get(WowUrlProvider.gateway.wowUserAuthorities.replace('{userUuid}', uuid.toUpperCase())).pipe(
      map((authorities) => {
       user.authorities = authorities;
       return user;
      })
    );
  }
  • Related