SwitchMap:类型 'void' 不可分配给类型 'ObservableInput<any>' 以进行 http 响应
SwitchMap: Type 'void' is not assignable to type 'ObservableInput<any>' for http response
我使用 rxjs v. 6.4.0 并尝试使用 switchMap
将我的 observable 的值传递给另一个 observable 但我收到错误 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"))
我错过了什么? getFoodOrder
returns 可观察。
console.log() 没有 return 任何错误。记录后返回数据将解决错误。
switchMap(data => { console.log(data) return of(data)});
如果只想记录数据,最好使用 tap
运算符,只看数据。
this.foodOrderProvider.getFoodOrder(1)
.pipe(
tap(data => console.log(data))
).subscribe(() => console.log("done"))
我使用 rxjs v. 6.4.0 并尝试使用 switchMap
将我的 observable 的值传递给另一个 observable 但我收到错误 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"))
我错过了什么? getFoodOrder
returns 可观察。
console.log() 没有 return 任何错误。记录后返回数据将解决错误。
switchMap(data => { console.log(data) return of(data)});
如果只想记录数据,最好使用 tap
运算符,只看数据。
this.foodOrderProvider.getFoodOrder(1)
.pipe(
tap(data => console.log(data))
).subscribe(() => console.log("done"))