我很难在使用HttpClient的Angular应用程序中实现递归HTTP请求功能。
我有一个API为我提供一些数据。API将数据放入页面中,这意味着第一个响应包含下一个页面的URL等等。(这是对公共HAPIFHIR服务器的搜索请求(
我在网上找到了这个链接,解释了我如何实现这一点,但它对我不起作用: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"){