【问题标题】:How to map an Observable with values from another observable如何将 Observable 与另一个 observable 的值映射
【发布时间】:2021-12-28 09:51:23
【问题描述】:

我在这里获得我的 Ofertas

getOfertasByYear(year:number): Observable<Oferta[]> {
return this.http.get<Oferta[]>(`${this.urlWebAPI}/ofertas/year/${year}`)
  .pipe(
    map(ofertas=>
      ofertas.map(oferta=>({
        ...oferta,
        añoPresentada:new Date(oferta.fechaPresentacionFulcrum).getFullYear(),
        organismoId:¿¿¿???
      }) as Oferta)
      ),
    tap(data => console.log('OfertasService-getOfertasByYear(): ', data)
    ),
    catchError(this.handleError)
  )
}

但我需要计算他的有机体,就在这里

getOrganismoDeOferta(ofertaId:string): Observable<Organismo> {
return this.http.get<Organismo>(`${this.urlWebAPI}/organismos/oferta/${ofertaId}`)
  .pipe(
    tap(//data=>console.log('OfertasService-getOrganismos(): ', data)
    ),
    catchError(this.handleError)
  )
}

而且我不知道如何将此 Observable 的结果传递给映射属性

getOfertasByYear(year:number): Observable<Oferta[]> {
return this.http.get<Oferta[]>(`${this.urlWebAPI}/ofertas/year/${year}`)
  .pipe(
    map(ofertas=>
      ofertas.map(oferta=>({
        ...oferta,
        añoPresentada:new Date(oferta.fechaPresentacionFulcrum).getFullYear(),
        organismoId:this.getOrganismoDeOferta(oferta.id).subscribe(data=>{
          ¿¿¿¿??????
        })
      }) as Oferta)
      ),
    tap(data => console.log('OfertasService-getOfertasByYear(): ', data)
    ),
    catchError(this.handleError)
  )
 }

我订阅了,但我不知道如何分配

我已尝试获得所有 Ofertas 和所有 Concursos,但都没有

ofertas$ = this.dataService.getOfertas();
concursos$ = this.dataService.getConcursos();

ofertasConOrganismos$ = forkJoin([
 this.ofertas$,
 this.concursos$
])
.pipe(
  map(([ofertas, concursos]) =>
    ofertas.map(oferta => ({
      ...oferta,
      organismoId: concursos.find(c => c.id ==                oferta.concursoId).organismoId
    }) as Oferta))
);

但我收到此错误:

Cannot read properties of undefined (reading 'organismoId')

有什么想法吗?

谢谢

【问题讨论】:

    标签: rxjs observable angular-observable


    【解决方案1】:

    您可以使用“Higher-Order Mapping Operator”为您处理内部订阅,而不是使用普通的map 然后调用.subscribe()。在这种情况下,让我们使用switchMap

    这个想法是在switchMap 中返回一个可观察的对象,它会发出您需要的数据。由于您需要拨打多个电话,我们可以利用forkJoin 的一些帮助。

    使用forkJoin,你传入一个可观察的数组,它会发出一个结果数组。所以下面我们将Oferta 的数组映射到一个observable 数组,每个observable 都会发出Oferta 并附加organismoId

    getOfertasByYear(year: number): Observable<Oferta[]> {
        return this.http.get<Oferta[]>(`${this.urlWebAPI}/ofertas/year/${year}`).pipe(
            switchMap(ofertas => forkJoin(
                ofertas.map(oferta => this.appendOrganismo(oferta))
            )),
            catchError(this.handleError)
        )
    }
    

    appendOrganismo() 的定义没什么花哨的;我们只需进行 http 调用,然后将结果映射到所需的形状:

    private appendOrganismo(oferta: Oferta) {
        return this.getOrganismoDeOferta(oferta.id).pipe(
            map(organismo => ({
                ...oferta,
                añoPresentada: new Date(oferta.fechaPresentacionFulcrum).getFullYear(),
                organismoId: organismo.id
            }))
        );
    }
    

    【讨论】:

    • @BizzBob 感谢您的精彩解释,但现在我在organismoId:organismo.id 行中的appendOrganismo() 中有一个错误:无法读取null 的属性(读取'id')。我不明白您调用 getOrganismoDeOferta 但从不订阅返回的数据?
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多