Home > database >  SwitchMap: Type 'void' is not assignable to type 'ObservableInput<any>' fo
SwitchMap: Type 'void' is not assignable to type 'ObservableInput<any>' fo

Time:12-03

I using rxjs v. 6.4.0 and am trying to use switchMap to pass the value of my observable to another observable but am getting the error Type 'void' is not assignable to type 'ObservableInput<any>'.

 getFoodOrder(id : number): Observable<IFoodOrder> {
    let url = `${ this.baseUrl }/food_orders/${ id }`
    return this.http.get<IFoodOrder>(url)
  }

  this.foodOrderProvider.getFoodOrder(1)
    .pipe(
      switchMap(data => console.log(data)) // <-- error occurs here
    ).subscribe(() => console.log("done"))

What am I missing? getFoodOrder returns an observable.

CodePudding user response:

console.log() does not return anything hence the error. Returning data after logging will resolve the error.

 switchMap(data =>  { console.log(data) return of(data)}); 

If you only want to log the data, better use tap operator, just looking at the data.

this.foodOrderProvider.getFoodOrder(1)
    .pipe(
      tap(data => console.log(data))
    ).subscribe(() => console.log("done"))
  • Related