[英]switchMap Type void is not assignable to type ObservableInput<{}>
[英]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
返回一個 observable。
console.log() 不返回任何內容,因此出現錯誤。 記錄后返回數據將解決錯誤。
switchMap(data => { console.log(data) return of(data)});
如果您只想記錄數據,最好使用tap
運算符,只查看數據。
this.foodOrderProvider.getFoodOrder(1)
.pipe(
tap(data => console.log(data))
).subscribe(() => console.log("done"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.