Angular 递归 HTTP 请求导致无限循环
Angular Recursive HTTP Request results in infinite loop
我很难在使用 HttpClient 的 Angular 应用程序中实现递归 HTTP 请求功能。
我有一个 API 可以为我提供一些数据。 API 将数据放入页面,这意味着第一个响应包含到下一页的 URL 等等。 (这是对 public HAPI FHIR 服务器的搜索请求)
我在网上找到了这个 link 解释我如何实现这一点,但它对我不起作用:
https://medium.com/angular-in-depth/rxjs-understanding-expand-a5f8b41a3602
这是我目前得到的:
为我服务class:
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"){
我很难在使用 HttpClient 的 Angular 应用程序中实现递归 HTTP 请求功能。
我有一个 API 可以为我提供一些数据。 API 将数据放入页面,这意味着第一个响应包含到下一页的 URL 等等。 (这是对 public HAPI FHIR 服务器的搜索请求)
我在网上找到了这个 link 解释我如何实现这一点,但它对我不起作用: https://medium.com/angular-in-depth/rxjs-understanding-expand-a5f8b41a3602
这是我目前得到的:
为我服务class:
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"){