Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'void | Observable<any>' is not assignable to type 'ObservableInput<any>'. Type 'void' is not assignable to type 'ObservableInput<any>'

I got this error in visual studio code:

Argument of type '(query: string) => void | Observable' is not assignable to parameter of type '(value: string, index: number) => ObservableInput'. Type 'void | Observable' is not assignable to type 'ObservableInput'. Type 'void' is not assignable to type 'ObservableInput'

Check code:

  public searchVal(val: string) {
    this.suggestions = (new Observable((observer: Observer<string>) => {
      observer.next(val);
    })).pipe(
      switchMap((query: string) => {
        if (query) { 
          return this.returnApiCall(query);  //THIS IS WORK WITH ONLY ONE RETURN
        }
        return of([]);
      })
    );
  }

BUT this is no work with if :

  public searchVal(val: string) {
    this.suggestions = (new Observable((observer: Observer<string>) => {
      observer.next(val);
    })).pipe(
      switchMap((query: string) => {
        if (query) { 
          if(this.serverApi === 'Driver'){
             return this.getAllDrivers(query);
          }else if(this.serverApi === 'Vehicle'){
             return this.getVehicle(query);
           }
        }
        return of([]);
      })
    );
  }

when I try to return some other api- calls does not work?

My api call is :

  returnApiCal(query: string) {
    return this.accountsService.getDrivers(query)
      .pipe(
        map((data: any) => {  
          return data.body || [];
        }),
        tap(() => noop, err => {
          this.errorMessage = err && err.message || 'Something goes wrong';
        })
      )
  }

In this line is red line :

  switchMap((query: string) => { //here
like image 592
Garibald Avatar asked Oct 26 '25 18:10

Garibald


1 Answers

Make sure that your switchMap's callback function allways returns something:

 public searchVal(val: string) {
    this.suggestions = (new Observable((observer: Observer<string>) => {
      observer.next(val);
    })).pipe(
      switchMap((query: string) => {
        if (query) { 
          if(this.serverApi === 'Driver'){
             return this.getAllDrivers(query);
          }else if(this.serverApi === 'Vehicle'){
             return this.getVehicle(query);
           }
          else { // you need to provide else here
            // return an observable when the two above conditions were not satisfied
          }
        }
        return of([]);
      })
    );
  }
like image 179
Saber Bjeoui Avatar answered Oct 29 '25 08:10

Saber Bjeoui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!