如何将所有页面合并为一个结果

huangapple go评论103阅读模式
英文:

How to get all pages into a single result

问题

I have a following API call

  1. this.httpCall.getAllItems().subscribe(results => {
  2. for (const each of results) {
  3. .....
  4. }
  5. ....
  6. });

getAllItems() 是一个基本的HTTP API调用,默认返回10条记录。总数在标头 X-Total-Count 中,例如为65

  1. getAllItems(): Observable<any> {
  2. const url = `URL/items?count=true`;
  3. return this.http.get(url, { observe: 'response' }));
  4. }

总数在 results.headers.get('X-Total-Count'); 中获取。

要获取下一页,需要在API中发送偏移量。因此,要获取所有65条记录,可以这样做

  1. URL/items?limit=10&offset=0
  2. URL/items?limit=10&offset=10
  3. URL/items?limit=10&offset=20
  4. URL/items?limit=10&offset=30
  5. URL/items?limit=10&offset=40
  6. URL/items?limit=10&offset=50
  7. URL/items?limit=10&offset=60

  1. URL/items?limit=10&offset=70

时将返回0个。

如何在 this.httpCall.getAllItems().subscribe(results => 中实现上述逻辑,以便将所有结果合并在一起,然后进行for循环?

我了解到switchMap可能对此有所帮助,但我需要一些关于如何在上述逻辑中实现它的帮助。

英文:

I have a following API call

  1. this.httpCall.getAllItems().subscribe(results => {
  2. for (const each of results) {
  3. .....
  4. }
  5. ....
  6. });

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

  1. getAllItems(): Observable<any> {
  2. const url = `URL/items?count=true`;
  3. return this.http.get(url, { observe: 'response' }));
  4. }

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

  1. URL/items?limit=10&offset=0
  2. URL/items?limit=10&offset=10
  3. URL/items?limit=10&offset=20
  4. URL/items?limit=10&offset=30
  5. URL/items?limit=10&offset=40
  6. URL/items?limit=10&offset=50
  7. URL/items?limit=10&offset=60

At

  1. 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

首先,您需要调用带有偏移量参数的函数以获取项目。

  1. getItems(offset: number): Observable<any> {
  2. const url = `URL/items`;
  3. return this.http.get(url, { 'limit': 10, 'offset': offset }));
  4. }

您还需要调用以获取总记录数的函数。

  1. getNumberOfItems(): Observable<any> {
  2. const url = `URL/items?count=true`;
  3. return this.http.get(url, { observe: 'response' }));
  4. }

然后,订阅它并在循环中使用偏移量作为参数调用getItems函数。

  1. this.httpCall.getNumberOfItems().subscribe(result: any => {
  2. let allItems = []
  3. const totalCount = result.headers.get('X-Total-Count');
  4. const iterations = Math.ceil(totalCount/10)
  5. for (let i = 0; i < iterations; i++) {
  6. this.httpCall.getItems(i*10).subscribe(listOfItems: any => {
  7. allItems.concat(listOfItems)
  8. })
  9. }
  10. });

这里查找ceil的用法
以及这里查找concat的用法

英文:

First of all you will need a call to get items with offset as a parameter.

  1. getItems(offset : number): Observable&lt;any&gt; {
  2. const url = `URL/items`;
  3. return this.http.get(url, { &#39;limit&#39;: 10, &#39;offset&#39;: offset }));
  4. }

you will also need a call to get the number of total records

  1. getNumberOfItems(): Observable&lt;any&gt; {
  2. const url = `URL/items?count=true`;
  3. return this.http.get(url, { observe: &#39;response&#39; }));
  4. }

then subscribe to it and call the getItems function with offset as parameter in a for loop

  1. this.httpCall.getNumberOfItems().subscribe(result: any =&gt; {
  2. let allItems = []
  3. const totalCount = result.headers.get(&#39;X-Total-Count&#39;);
  4. const iterations = Math.ceil(totalCount/10)
  5. for (let i = 0; i &lt; iterations; i++) {
  6. this.httpCall.getItems(i*10).subscribe(listOfItems: any =&gt; {
  7. allItems.concat(listOfItems)
  8. })
  9. }
  10. });

Find here what ceil does
and here what concat does

huangapple
  • 本文由 发表于 2023年4月13日 14:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定