Home > Software design >  Facing problem with return Observable<boolean> Angular
Facing problem with return Observable<boolean> Angular

Time:09-21

I'm facing a problem with angular.. I created a pipe that is supposed to call another pipe and check with a loop if all the elements of an array are false.. if one is true ill return true.. I don't know why I'm not allowed to return an Observable boolean..
I have been struggling the whole day with this error.. I would really appreciate if someone could help me The Error:

TS2322: Type 'Observable' is not assignable to type 'Observable'.   Type 'void' is not assignable to type 'boolean'.

transform(antwortElem: UiKategorie[]): Observable<boolean> {
      if(!antwortElem){
         return null;
      }
      return this.iqsService.enabledStateInbearbeitung$.pipe(
         map(enabledState => {
            let enabled = false;
            antwortElem.forEach((antwort) => {
               if(enabledState[antwort.struktur.id] != null){
                  return enabledState[antwort.struktur.id];
               }else{
                  return true;
               }
            })

         })
      )


   }

CodePudding user response:

transform(antwortElem: UiKategorie[]): Observable<boolean> {
  if(!antwortElem){
     return null; // this probably isn't allowed for your typings. needs to be Observable<boolean> | null
  }
  return this.iqsService.enabledStateInbearbeitung$.pipe(
     map(enabledState => {
        // for each doesn't work that way, use the some operator instead
        // some will return true if any element matches the condition
        return antwortElem.some(antwort => !!enabledState[antwort.struktur.id])
     })
  )
}

CodePudding user response:

map needs to return something. In your case you did a forEach which should be returned

 for(let antwort in antwortElem) {
               if(enabledState[antwort.struktur.id] != null){
                  return enabledState[antwort.struktur.id];
               }else{
                  return true;
               }
            })

The error states that you return nothing and that's why it is void

  • Related