How to pipe / map an Observable in Angular

后端 未结 2 1852
臣服心动
臣服心动 2021-01-02 02:59

A nested object is showing up as [object Object] so I\'m trying to cast it via pipe and map but I\'m not getting any where. I\'ve tried the models as classes and interfaces

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 03:37

    1) remove the piping part from your getClients() method

    2) do the pipe-map before subscribing to getClients() or create another method, that will do only the piping part with the observable returned from getClients()

    mapToAddress(): Observable {
      this.getClients.pipe(
        map((clients: Client[]) => clients.map(client => client.address))
      )
    }
    

    This is important to understand: when you call .map() method inside .pipe(), you're not getting a single client in this case, you get the whole clients array, pushed to Observable. Because you map the values, that are stored in the Observable - the values of type: < Client[] >.

    Your pipe-map would work on some Observable, that emits a single client of type < Client >, not an array.

提交回复
热议问题