0

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?

1
  • Try instead of (authorities) =>user.authorities = authorities to use (authorities) => {...user, authorities} Commented Aug 11, 2022 at 13:39

1 Answer 1

1
getUser(uuid: string): Observable<WowUserDataModel> {
  // ...
    return this.http.get(WowUrlProvider.gateway.wowUserAuthorities.replace('{userUuid}', uuid.toUpperCase())).pipe(
      map((authorities) => {
       user.authorities = authorities;
       return user;
      })
    );
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Geee, so simple! I was trying to return of(user) this whole time. Thank you, you saved my day!
@PaulJK If your code was trying to return of(user), why didn't you mention that in your question? Please be more specific in what you have tried.
It's not the code, it's me. I simply assumed that user isn't an observable without checking it, and I was trying to make it an observable manually and getting errors. Feeling real dumb right now.
@PaulJK don't worry, it takes practice to know really well rxjs operators, just keep practicing, also, if you use map as shown in your example, whatever you send inside of it, is an observable, no need of returning of(...) unless you want a double observable...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.