如何在多个页面上获取数据? [英] How to fetch data over multiple pages?

查看:118
本文介绍了如何在多个页面上获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目基于React,redux,redux-saga,es6,我尝试从此API获取数据:

http://api.dhsprogram.com/rest/dhs/data/BD,2000,2004,2007?&returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json

如您所见,此特定的API调用显示的数据限制为每页100个数据,分布在40个页面上.

根据此答案: http://userforum.dhsprogram. com/index.php?t = msg& th = 2086& goto = 9591& S = Google 它表示您可以将限制扩展到每页最多3000个数据.

但是,在某些情况下,我会进行超出该限制的API调用,这意味着我将无法像这样接收所有数据:

export function fetchMetaData(countryCode: string, surveyYears: string) {
return (fetch('http://api.dhsprogram.com/rest/dhs/data/' + countryCode + ',' + surveyYears + '?returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json')
    .then(response => response.json())
    .then(json => json.Data.map(survey => survey)))
} 

所以我的问题是;考虑到我知道数据的总页数,从此API获取所有数据的最佳方法是什么.论坛链接中的答案建议通过API循环.但是,我找不到正确的语法用法来做到这一点.

我的想法是进行一次api调用以获取总页数.然后使用redux + redux-saga将其存储在状态中.然后执行一个新请求,将总页数作为参数发送,并获取此总页数.而且通过这种方式,我无法弄清楚存储每次迭代数据的语法.

解决方案

一个可能的解决方案-想法是先获取页面数,然后进行适当数量的API调用,将每个调用的承诺都推入数组中.然后,我们等待所有承诺都解决,并对返回的数据进行处理.

function fetchMetaData() {
    let pagesRequired = 0;

    fetch('apiUrlToGetPageNumber')
    .then(resp = > {
        const apiPromises = [];
        pagesRequired = resp.data.pagesRequired;

        for (let i=pagesRequired; i>0;i--) {
            apiPromises.push(fetch('apiUrlToSpecificPage?page = ' + i));
        }

        Promise.all(apiPromises)
        .then(responses => {
            const processedResponses = [];
            responses.map(response => {
                processedResponses.push(response);
            }

            // do something with processedResponses here
        });
    }
}

My project is based on React, redux, redux-saga, es6 and I try to fetch data from this API:

http://api.dhsprogram.com/rest/dhs/data/BD,2000,2004,2007?&returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json

As you can see, this specific API call shows data with a limit of 100 data per page spread on 40 pages.

According to this answer: http://userforum.dhsprogram.com/index.php?t=msg&th=2086&goto=9591&S=Google it says that you can extend the limit to a maximum of 3000data per page.

However, in some cases I would do an API call that exceeds that limit which means I would not receive all my data doing it like this:

export function fetchMetaData(countryCode: string, surveyYears: string) {
return (fetch('http://api.dhsprogram.com/rest/dhs/data/' + countryCode + ',' + surveyYears + '?returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json')
    .then(response => response.json())
    .then(json => json.Data.map(survey => survey)))
} 

So my question is; what is the best way to get all data from this API given that I know the total pages of data. The answer in the forum link suggest to loop through the API. However, I can't find the right syntax usage to do this.

My idea would be doing one api call to get the total number of pages. Then store this in a state using redux+redux-saga. Then do a new request sending the total pages as parameter and fetch this total number of pages times. And by doing this I can't figure out the syntax to store the data for each iteration.

解决方案

A possible solution -the idea is to get the number of pages first, then make the appropriate number of API calls, pushing the promise from each call into an array. We then wait for all the promises to resolve, and do something with the returned data.

function fetchMetaData() {
    let pagesRequired = 0;

    fetch('apiUrlToGetPageNumber')
    .then(resp = > {
        const apiPromises = [];
        pagesRequired = resp.data.pagesRequired;

        for (let i=pagesRequired; i>0;i--) {
            apiPromises.push(fetch('apiUrlToSpecificPage?page = ' + i));
        }

        Promise.all(apiPromises)
        .then(responses => {
            const processedResponses = [];
            responses.map(response => {
                processedResponses.push(response);
            }

            // do something with processedResponses here
        });
    }
}

这篇关于如何在多个页面上获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆