尝试对API(Angular、TypeScript、RxJS)发出多个请求时出现TypeScript可见错误
我得到这个错误:尝试对API(Angular、TypeScript、RxJS)发出多个请求时出现TypeScript可见错误,angular,typescript,rxjs,observable,Angular,Typescript,Rxjs,Observable,我得到这个错误: ERROR in src/app/fetch-trefle.service.ts:86:31 - error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 86 mergeMap((item: any): Observable<any> => { 我试图向TrefleAPI发出两个请求,并从每个JSON响应中收集
ERROR in src/app/fetch-trefle.service.ts:86:31 - error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
86 mergeMap((item: any): Observable<any> => {
我试图向TrefleAPI发出两个请求,并从每个JSON响应中收集多个值。当我执行单个请求时,它工作得很好,当我重构为执行多个请求时,它在顶部给出了TS错误。我假设这个问题涉及语法或一些我不知道的行为或可观察对象和RxJS
谢谢大家! 您的mergeMap块需要返回一个可观察值的返回 编辑,因为我错误地按了“发送” 我想你需要的只是一个水龙头或一个玩具。mergeMap希望收到一个可观察到的消息以继续该流 将您的合并地图替换为以下内容:
tap((项目:任意)=>
this.id=item.id
...
)
您需要从合并映射体返回observable
const click$ = fromEvent(document, 'click');
click$
.pipe(
mergeMap((e: MouseEvent) => {
return of({
x: e.clientX,
y: e.clientY,
timestamp: Date.now()
});
})
)
fetchAllPlantData()看起来很奇怪。。你不可能有2次退货..是的!我需要一个
返回
。下面是有效的代码:`getPlantGrowth():Observable{返回this.http.get(this.proxyurl+this.growthull+this.page)。pipe(pull(“数据”)、pull(“主要物种”)、map((项:any)=>{return[this.id,this.common\u name,this.scientific\u name,this.maximum\u detacient,this.minimum\u detacient]=[item.id,item.common\u name,item.growth.maximum\u detacient.mm,item.growth.minimum\u detacient.mm]}`
import { Component } from '@angular/core'
import { FetchTrefleService } from './fetch-trefle.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
plants: any
constructor(private fetchTrefleService: FetchTrefleService) { }
getAllPlants() {
this.fetchTrefleService.fetchAllPlantData().subscribe(res => {
console.log(res)
this.plants = res
})
}
}
const click$ = fromEvent(document, 'click');
click$
.pipe(
mergeMap((e: MouseEvent) => {
return of({
x: e.clientX,
y: e.clientY,
timestamp: Date.now()
});
})
)