英文:
How to get all pages into a single result
问题
I have a following API call
this.httpCall.getAllItems().subscribe(results => {
for (const each of results) {
.....
}
....
});
getAllItems()
是一个基本的HTTP API调用,默认返回10条记录。总数在标头 X-Total-Count
中,例如为65
getAllItems(): Observable<any> {
const url = `URL/items?count=true`;
return this.http.get(url, { observe: 'response' }));
}
总数在 results.headers.get('X-Total-Count');
中获取。
要获取下一页,需要在API中发送偏移量。因此,要获取所有65条记录,可以这样做
URL/items?limit=10&offset=0
URL/items?limit=10&offset=10
URL/items?limit=10&offset=20
URL/items?limit=10&offset=30
URL/items?limit=10&offset=40
URL/items?limit=10&offset=50
URL/items?limit=10&offset=60
在
URL/items?limit=10&offset=70
时将返回0个。
如何在 this.httpCall.getAllItems().subscribe(results =>
中实现上述逻辑,以便将所有结果合并在一起,然后进行for循环?
我了解到switchMap
可能对此有所帮助,但我需要一些关于如何在上述逻辑中实现它的帮助。
英文:
I have a following API call
this.httpCall.getAllItems().subscribe(results => {
for (const each of results) {
.....
}
....
});
The getAllItems()
is a basic HTTP API call which returns a result of 10 records by default. The total count comes in header X-Total-Count
which for example is 65
getAllItems(): Observable<any> {
const url = `URL/items?count=true`;
return this.http.get(url, { observe: 'response' }));
}
The total count comes in results.headers.get('X-Total-Count');
.
To get the next page I need to send offset in API. So, to get all 65 records, I would do
URL/items?limit=10&offset=0
URL/items?limit=10&offset=10
URL/items?limit=10&offset=20
URL/items?limit=10&offset=30
URL/items?limit=10&offset=40
URL/items?limit=10&offset=50
URL/items?limit=10&offset=60
At
URL/items?limit=10&offset=70
There will be 0 return.
How do I implement this above in this.httpCall.getAllItems().subscribe(results =>
so that I can join all the results in one and then do the for loop continuation?
I read that perhaps switchMap
can help here, but I need some help on how to implement it in above logic.
答案1
得分: 1
首先,您需要调用带有偏移量参数的函数以获取项目。
getItems(offset: number): Observable<any> {
const url = `URL/items`;
return this.http.get(url, { 'limit': 10, 'offset': offset }));
}
您还需要调用以获取总记录数的函数。
getNumberOfItems(): Observable<any> {
const url = `URL/items?count=true`;
return this.http.get(url, { observe: 'response' }));
}
然后,订阅它并在循环中使用偏移量作为参数调用getItems函数。
this.httpCall.getNumberOfItems().subscribe(result: any => {
let allItems = []
const totalCount = result.headers.get('X-Total-Count');
const iterations = Math.ceil(totalCount/10)
for (let i = 0; i < iterations; i++) {
this.httpCall.getItems(i*10).subscribe(listOfItems: any => {
allItems.concat(listOfItems)
})
}
});
英文:
First of all you will need a call to get items with offset as a parameter.
getItems(offset : number): Observable<any> {
const url = `URL/items`;
return this.http.get(url, { 'limit': 10, 'offset': offset }));
}
you will also need a call to get the number of total records
getNumberOfItems(): Observable<any> {
const url = `URL/items?count=true`;
return this.http.get(url, { observe: 'response' }));
}
then subscribe to it and call the getItems function with offset as parameter in a for loop
this.httpCall.getNumberOfItems().subscribe(result: any => {
let allItems = []
const totalCount = result.headers.get('X-Total-Count');
const iterations = Math.ceil(totalCount/10)
for (let i = 0; i < iterations; i++) {
this.httpCall.getItems(i*10).subscribe(listOfItems: any => {
allItems.concat(listOfItems)
})
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论