如何解决Angular递归HTTP请求导致无限循环
我很难使用HttpClient在Angular应用程序中实现递归HTTP请求功能。
我有一个API,可以为我提供一些数据。 API将数据放入页面中,这意味着第一个响应包含指向下一页的URL,依此类推。 (这是对公共HAPI FHIR服务器的搜索请求)
我在网上找到了此链接,解释了如何实现此目的,但是它对我不起作用: https://medium.com/angular-in-depth/rxjs-understanding-expand-a5f8b41a3602
这是我现在得到的:
在我的服务班级:
getFhirVitalSigns(patientId: string): Observable<any> {
let url = this.vitalSignsUrl + "?subject:Patient=" + patientId;
console.log("Starting process for getting all vital signs")
return this.getPage(url).pipe(
expand(({next}) => next ? this.getPage(next) : empty()),concatMap(({content}) => content)
);
}
getPage(url:string): Observable<{
content: any[];
next: string | null;
}> {
return this.http.get(url).pipe(
map((response:any) => ({
content: response.entry,next: this.getNextPage(response)
}))
);
}
getNextPage(response: any): string | null {
let url: string | null = null;
for (let link of response.link){
if (link.relation = "next"){
url = link.url;
}
}
console.log("Found new page: " + url);
return url;
}
this.vitalSignsService.getFhirVitalSigns(this.patientId!).subscribe(
(test) => console.log(test)
);
现在的问题是:代码以无限循环结尾。看起来当不再有新页面时,代码无法实现。
有人可以帮我吗?
这是页面的外观:
{
"resourceType": "Bundle","id": "67ea2654-e1a6-4d21-b242-66c99024df64","Meta": {
"lastUpdated": "2020-09-02T19:32:30.124+00:00"
},"type": "searchset","total": 30,"link": [
{
"relation": "self","url": "http://hapi.fhir.org/baseR4/Observation?_count=1&subject%3APatient=1457293"
},{
"relation": "next","url": "http://hapi.fhir.org/baseR4?_getpages=67ea2654-e1a6-4d21-b242-66c99024df64&_getpagesoffset=1&_count=1&_pretty=true&_bundletype=searchset"
}
],"entry": [
{
"fullUrl": "http://hapi.fhir.org/baseR4/Observation/1457319","resource": {
// actual data of first data item
}
]
}
解决方法
请尝试更换
if (link.relation = "next"){
使用
if (link.relation == "next"){
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。