4

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

1 Answer 1

5

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([]);
      })
    );
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.