Gaurav Matta
Gaurav Matta

Reputation: 65

update observable object from array in observable object

I have documents id in the Information I am getting from service. I need documents Info to be attached to this parent information and return parent. Not able to return observables in loop.

The second switch map is working fine. The first is not executing if I return tInfo from map, it gives following error below.

Argument of type '(tInfo: { documents; documentsInfo: any[]; }) => void' is not assignable to parameter of type '(value: { documents: any; documentsInfo: any[]; }, index: number) => ObservableInput'. Type 'void' is not assignable to type 'ObservableInput'

public transform(tId: any,name: string, args?: any): Observable<string> {
  return this.sService.getTaxById(tId).pipe(
    // tslint:disable-next-line: deprecation
    switchMap((tInfo:{ documents ; documentsInfo: any [] }) => {
      if (tInfo.documents) {
        tInfo..documents.forEach(doc => {
          return this.sService
            .getFiles(doc.docId).pipe(
              map((filesInfo: { FileName: ''; downloadUrl: '' }) => {
                        const fileObjnew =
                          {
                            docName: filesInfo.FileName,
                            downloadUrl: filesInfo.downloadUrl,
                          }
                          tInfo.documentsInfo.push(fileObjnew);
                      })
            );
        });
       return of(taxInfo);
      }
    }),
    // tslint:disable-next-line: deprecation
    switchMap((Info: { cc: '' ; authority: '' ; cName: ''}) => {
      if (Info.authority) {
        const cc = taxInfo.cc;
        return this.sService.getOrgs(cc).pipe(
          map((orgsData: any) => {
            console.log("Data============>", orgsData);
            (orgsData || []).forEach(tax => {
              if (orgsData.aid === tax.id) {
                Info.cName = tax.parentName;
              }
            });
            return Info;
          })
        );
      }
    }),
    map((Info) => {
      if (name === 'subsTemplate') {
        return this.subsTemplate(Info);
      }
    })
  );
}

Upvotes: 0

Views: 740

Answers (1)

Amer
Amer

Reputation: 6706

In the first switchMap you have to return an observable in all code paths and to chain the getFiles sub-observables to your main observable, then return the observable combined by forkJoin.

You can try the following:

public transform(tId: any, name: string, args?: any): Observable<string> {
  return this.sService.getTaxById(tId).pipe(
    // tslint:disable-next-line: deprecation
    switchMap((tInfo: { documents; documentsInfo: any[] }) => {
      if (tInfo.documents) {
        return forkJoin(
          tInfo.documents.map((doc) =>
            this.sService.getFiles(doc.docId).pipe(
              map((filesInfo: { FileName: ''; downloadUrl: '' }) => {
                const fileObjnew = {
                  docName: filesInfo.FileName,
                  downloadUrl: filesInfo.downloadUrl,
                };
                tInfo.documentsInfo.push(fileObjnew);
              })
            )
          )
        ).pipe(mapTo(taxInfo));
      }
      return of(taxInfo);
    }),
    // tslint:disable-next-line: deprecation
    switchMap((Info: { cc: ''; authority: ''; cName: '' }) => {
      if (Info.authority) {
        const cc = taxInfo.cc;
        return this.sService.getOrgs(cc).pipe(
          map((orgsData: any) => {
            console.log('Data============>', orgsData);
            (orgsData || []).forEach((tax) => {
              if (orgsData.aid === tax.id) {
                Info.cName = tax.parentName;
              }
            });
            return Info;
          })
        );
      }
      return of(null);
    }),
    map((Info) => {
      if (name === 'subsTemplate') {
        return this.subsTemplate(Info);
      }
    })
  );
}

Upvotes: 1

Related Questions