property not found mapping combining streams with RxJS in angular

Viewed 36

Hi I have this two streams with Ofertas and concursos

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

ofertasConOrganismos$ = forkJoin([
 this.ofertas$,
 this.concursos$
]);

ngOnInit(): void {
this.ofertasConOrganismos$
  .subscribe(item => console.log(item));

}

streams

In Ofertas[] each Oferta has organismoId Property that I need to fill with the relationed value en Concurso[] that has this property too

organismoId

concurso

But when I try this

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))
);

I get this error

error

Any idea, please?

Thanks

1 Answers

problem is with "concursos.find(c => c.id == oferta.concursoId)" that is undefined - the "find()" method didn't find anything so you cannot retrieve ".organismoId" parameter on undefined object. You can use following optional operator syntax (with the question mark) in order to avoid the error:

map(([ofertas, concursos]) =>
ofertas.map(oferta => ({
  ...oferta,
  organismoId: concursos.find(c => c.id == oferta.concursoId)?.organismoId
}) as Oferta))
Related