简体   繁体   English

Angular:在返回结果之前等待 function 内的 Observable

[英]Angular: wait for Observable inside a function before returning result

I need help with the following function:我需要以下 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)我需要的是:等待this.http.api调用(返回Observable)完成,将结果分配给user.authorities,然后才返回整个用户的Observable ZA8CFDE6331BD4B66AC96F8911(单个用户C)

What is happening now: the function returns just authorities property, not the whole user object现在发生了什么: function 只返回权限属性,而不是整个用户 object

Can this be achieved without changing the return type of the function?这可以在不改变 function 的返回类型的情况下实现吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM