【发布时间】:2020-06-17 04:18:55
【问题描述】:
我在 ngOnInit 中实现了对两个 api 路由的调用以获取一些数据,我正在使用 angular 7 并实现 forkJoin 来像这样调用 api:
ngOnInit() {
debugger
this.route.params.pipe(
switchMap(params => forkJoin([
this.http.get('/api/surveys/' + params.id),
this.http.get('/api/surveys/getStatistics/' + params.id)
]))
).subscribe(result => {
this.survey = result[0];
this.statistics = result[1];
this.updateChart(result[1]);
},
error => this.handleError(error)
);
}
private handleError(error) {
debugger
if (error.status === 400) {
this.showError(error.error.error, 'Erreur!');
if (error.error.error === 'Le sondage demandé n\'existe plus!') {
this.router.navigateByUrl('/sondage');
}
}
}
private updateChart(statistics) {
debugger
statistics.options.forEach(option => {
this.pieData.push(option.number);
option.color = this.d3Colors[this.colorCounter];
this.colorCounter = this.colorCounter + 1;
});
}
在第一个调试器之后,代码不会运行对 API 的调用请求,而是直接传递给 handleError 函数!并生成 500 服务器错误
【问题讨论】:
-
真正的问题是什么?
-
@AndrewAllen 来自这里:stackoverflow.com/a/60526695/5367916。 OP 暗示使用
forkJoin会生成 500 错误。 -
@KurtHamilton 暗示!= 一个问题
-
没错。这就是我的意思...
标签: javascript angular api pipe fork-join