Javascript 多个API调用的问题
我写的ngrx效果有一些问题,它应该通过多个API调用检索信息。由于某些原因,它成功地随机检索了一些调用,但其他调用则返回null 效果:Javascript 多个API调用的问题,javascript,angular,typescript,ngrx-effects,Javascript,Angular,Typescript,Ngrx Effects,我写的ngrx效果有一些问题,它应该通过多个API调用检索信息。由于某些原因,它成功地随机检索了一些调用,但其他调用则返回null 效果: @Effect() loadMoveList$: Observable<Action> = this.actions$.pipe( ofType(pokemonActions.PokemonActionTypes.SetSelectedGame), withLatestFrom( this.store$.sele
@Effect()
loadMoveList$: Observable<Action> = this.actions$.pipe(
ofType(pokemonActions.PokemonActionTypes.SetSelectedGame),
withLatestFrom(
this.store$.select(pokemonSelectors.getMoveLists),
this.store$.select(pokemonSelectors.getSelectedGame),
),
map(([action, moveLists, selectedGame])=> {
let newMoveList = []
if(moveLists[selectedGame][0].moveInfo === null){
moveLists[selectedGame].map(_move=>{
newMoveList.push({..._move, moveInfo: this.pokemonService.getMove(_move.moveUrl)}as Move)
})
}
const newMoveLists = {...moveLists, [selectedGame]: newMoveList} as MoveLists
return new pokemonActions.LoadMoveListSuccess(newMoveLists)
})
)
getMove(moveUrl:string):Observable<MoveInfo>{
if(this.movesCache[moveUrl]){
return this.movesCache[moveUrl];
}
else{
this.movesCache[moveUrl] = this.httpClient.get<MoveInfo>(moveUrl).pipe(
shareReplay(1),
catchError(err=>{
delete this.movesCache[moveUrl];
console.log(err);
return EMPTY;
}));
}
}
@Effect()
loadMoveList$:Observable=此.actions$.pipe(
类型(pokemonActions.PokemonActionTypes.setSelectedName),
最晚从(
this.store$.select(pokemonSelectors.getMoveLists),
此.store$.select(pokemonSelectors.getSelectedName),
),
映射(([action,MoveList,SelectedName])=>{
让newMoveList=[]
如果(移动列表[SelectedName][0]。移动信息===null){
移动列表[SelectedName].map(\u move=>{
推送({…\u move,moveInfo:this.pokemonService.getMove(\u move.moveUrl)}作为移动)
})
}
const newMoveLists={…moveLists,[selectedGame]:newMoveList}作为移动列表
返回新的口袋表情。LoadMoveListSuccess(newMoveLists)
})
)
服务getMove()代码:
@Effect()
loadMoveList$: Observable<Action> = this.actions$.pipe(
ofType(pokemonActions.PokemonActionTypes.SetSelectedGame),
withLatestFrom(
this.store$.select(pokemonSelectors.getMoveLists),
this.store$.select(pokemonSelectors.getSelectedGame),
),
map(([action, moveLists, selectedGame])=> {
let newMoveList = []
if(moveLists[selectedGame][0].moveInfo === null){
moveLists[selectedGame].map(_move=>{
newMoveList.push({..._move, moveInfo: this.pokemonService.getMove(_move.moveUrl)}as Move)
})
}
const newMoveLists = {...moveLists, [selectedGame]: newMoveList} as MoveLists
return new pokemonActions.LoadMoveListSuccess(newMoveLists)
})
)
getMove(moveUrl:string):Observable<MoveInfo>{
if(this.movesCache[moveUrl]){
return this.movesCache[moveUrl];
}
else{
this.movesCache[moveUrl] = this.httpClient.get<MoveInfo>(moveUrl).pipe(
shareReplay(1),
catchError(err=>{
delete this.movesCache[moveUrl];
console.log(err);
return EMPTY;
}));
}
}
getMove(moveUrl:string):可观察{
if(this.movesCache[moveUrl]){
返回这个.movesCache[moveUrl];
}
否则{
this.movesCache[moveUrl]=this.httpClient.get(moveUrl.pipe)(
共享重播(1),
catchError(err=>{
删除这个.movesCache[moveUrl];
控制台日志(err);
返回空;
}));
}
}
我将重构服务方法getMove()
没有返回可观察的,并且在else块中没有返回http get调用的结果
重构服务方法
getMove(moveUrl:string):Observable<MoveInfo>{
if(this.movesCache[moveUrl]){
return of(this.movesCache[moveUrl]);
}
else{
return this.httpClient.get<MoveInfo>(moveUrl).pipe(
tap(move => this.moveCache[moveUrl] = move),
catchError(err=>{
delete this.movesCache[moveUrl];
console.log(err);
throwError(err.message)
}));
}
}
getMove(moveUrl:string):可观察{
if(this.movesCache[moveUrl]){
返回(this.movesCache[moveUrl]);
}
否则{
返回此.httpClient.get(moveUrl.pipe)(
点击(move=>this.moveCache[moveUrl]=move),
catchError(err=>{
删除这个.movesCache[moveUrl];
控制台日志(err);
投掷者(错误信息)
}));
}
}
重构效果
@Effect()
loadMoveList$: Observable<Action> = this.actions$.pipe(
ofType(pokemonActions.PokemonActionTypes.SetSelectedGame),
withLatestFrom(
this.store$.select(pokemonSelectors.getMoveLists),
this.store$.select(pokemonSelectors.getSelectedGame),
),
switchMap(([action, moveLists, selectedGame])=> {
let apiCalls = [];
if(moveLists[selectedGame][0].moveInfo === null) {
apiCalls = [moveLists[selectedGame].map(_move=> this.pokemonService.getMove(_move.moveUrl)]
return forkJoin(...apiCalls).pipe(
map((response) => {
let newMoveLists = moveLists[selectedGame].map((_move, index) => {
return { ..._move, moveInfo: response[index] }
})
return new pokemonActions.LoadMoveListSuccess(newMoveLists)
})
)
} else {
const newMoveLists = {...moveLists, [selectedGame]: []} as MoveLists
return new pokemonActions.LoadMoveListSuccess(newMoveLists)
}
})
)
@Effect()
loadMoveList$:Observable=此.actions$.pipe(
类型(pokemonActions.PokemonActionTypes.setSelectedName),
最晚从(
this.store$.select(pokemonSelectors.getMoveLists),
此.store$.select(pokemonSelectors.getSelectedName),
),
switchMap(([action,MoveList,SelectedName])=>{
设apicall=[];
如果(移动列表[SelectedName][0]。移动信息===null){
apiCalls=[MoveList[SelectedName].map(\u-move=>this.pokemonService.getMove(\u-move.moveUrl)]
返回forkJoin(…apicall).pipe(
映射((响应)=>{
让NewMoveList=MoveList[SelectedName].map((移动,索引)=>{
返回{…\u move,moveInfo:response[index]}
})
返回新的口袋表情。LoadMoveListSuccess(newMoveLists)
})
)
}否则{
const newMoveLists={…moveLists,[selectedGame]:[]}作为移动列表
返回新的口袋表情。LoadMoveListSuccess(newMoveLists)
}
})
)
在提供解决方案之前,我发现服务和效果中几乎没有问题,我想了解如果移动列表[selectedGame][0]。moveInfo==null