Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在没有';的情况下,通过Node.js在while循环中向API发出多个分页GET请求;我不知道页数?_Javascript_Json_Node.js_Rest_Asynchronous - Fatal编程技术网

Javascript 如何在没有';的情况下,通过Node.js在while循环中向API发出多个分页GET请求;我不知道页数?

Javascript 如何在没有';的情况下,通过Node.js在while循环中向API发出多个分页GET请求;我不知道页数?,javascript,json,node.js,rest,asynchronous,Javascript,Json,Node.js,Rest,Asynchronous,我使用的是RESTJSONAPI,它无法查询页面或条目的总数。但是我需要在一个循环中对API发出多个请求,以便获得所有可用的数据 在搜索了许多stackoverflow问题后,我能找到的最接近的工作解决方案成功地提出了多个请求,但仍然要求您知道最后一页是什么: 这项工作: const async = require("async"); const request = require("request"); let page = 1; let last_page = 20; let json;

我使用的是RESTJSONAPI,它无法查询页面或条目的总数。但是我需要在一个循环中对API发出多个请求,以便获得所有可用的数据

在搜索了许多stackoverflow问题后,我能找到的最接近的工作解决方案成功地提出了多个请求,但仍然要求您知道最后一页是什么:

这项工作:

const async = require("async");
const request = require("request");

let page = 1;
let last_page = 20;
let json;

async.whilst(function () {
    // condition here
  return page <= last_page
},
function (next) {
  request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page}&per_page=20`, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    json = JSON.parse(body);
        console.log(json.data);      
        console.log(page);
    }
    page++;
    next();
  });
},
function (err) {
  // All things are done!
});

我已经在这个问题上工作了相当长的时间了,所以任何帮助都将不胜感激!谢谢

正如我所说,我不是Node.js用户,所以不熟悉您正在使用的库,但这至少是一个总体想法

function getPage(page) {
  request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page}&per_page=20`, function (error, response, body) {
    if (!error && response.statusCode == 200) {

        // do whatever you do with the current page here.

        if (json.data.length == 20) {
            request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page + 1}&per_page=20`, function (error, response, body) {
                var json = JSON.parse(body);
                if (json.data.length == 0) {
                    // the next page has no items - disable the next button here
                }
            });
        }
        else {
            // this page has < 20 items - disable the next button here
        }
    }
  });
}
函数getPage(第页){
请求(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page}&per_page=20`,函数(错误、响应、正文){
如果(!error&&response.statusCode==200){
//在此处对当前页面执行任何操作。
if(json.data.length==20){
请求(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page+1}&per_page=20`,函数(错误、响应、正文){
var json=json.parse(body);
if(json.data.length==0){
//下一页没有项目-在此处禁用“下一步”按钮
}
});
}
否则{
//此页面包含<20项-禁用此处的“下一步”按钮
}
}
});
}

您调用该方法,它将获得由参数指定的页面,还将检查它是否是最后一页,并禁用“下一步”按钮(一旦您添加了执行该位的代码)。

您的逻辑存在一些问题。首先,您没有返回测试函数中的验证。但即使您这样做了,您也在针对未定义的变量进行测试。因此,验证将失败,并在第一次迭代之前退出代码

let oldPage = 1;
let nextPage = 2;
let json;

async.whilst(function () {
    // Check that oldPage is less than newPage
  return oldPage < nextPage;
},
function (next) {
  request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${oldPage}&per_page=20`, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    json = JSON.parse(body);
        console.log(json.data);      
        console.log(oldPage);
    }
    if (json.data.length) {
      // When the json has no more data loaded, nextPage will stop 
      // incrementing hence become equal to oldPage and return 
      // false in the test function.
      nextPage++;
    }
    oldPage++;
    next();
  });
},
function (err) {
  // All things are done!
});
让oldPage=1;
设nextPage=2;
让json;
async.while(函数(){
//检查oldPage是否小于newPage
返回旧页<下一页;
},
功能(下一个){
请求(`https://driftrock-dev-test-2.herokuapp.com/purchases?${oldPage}&per_page=20`,函数(错误、响应、正文){
如果(!error&&response.statusCode==200){
json=json.parse(body);
log(json.data);
控制台日志(旧页);
}
if(json.data.length){
//当json没有加载更多数据时,nextPage将停止
//因此,递增变为等于oldPage和return
//在测试函数中为false。
nextPage++;
}
oldPage++;
next();
});
},
功能(err){
//一切都完成了!
});

通过这种方式,您可以看到不再显示新页面的时刻。

问题似乎是您无法通过一次调用API获得结果否?更不用说最后一页了?同时获取当前页和+1页。如果第+1页返回0项,则禁用“下一步”按钮。您不需要知道有多少项,只需处理到最后一页即可。谢谢@Archer,这似乎是迄今为止最有用的提示,但您是否能够发布完整的解决方案?例如,要放入的正确逻辑是:
async.while(function(){//condition here}
我已经尝试过了。它至少应该为您指明了正确的方向。有一天我会抽出时间来使用Node.js!:pThanks Archer,这很有用,但我认为这里需要一些控制流逻辑,当`json.data.length为0时,它会以1停止对页面的迭代。您可以使用runkit进行尝试。(需要时自动包含依赖项)根据您的示例查看我的伪代码:谢谢,这很有效!我理解
return
部分,但是您可以在注释中快速解释为什么条件oldPagelet oldPage = 1; let nextPage = 2; let json; async.whilst(function () { // Check that oldPage is less than newPage return oldPage < nextPage; }, function (next) { request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${oldPage}&per_page=20`, function (error, response, body) { if (!error && response.statusCode == 200) { json = JSON.parse(body); console.log(json.data); console.log(oldPage); } if (json.data.length) { // When the json has no more data loaded, nextPage will stop // incrementing hence become equal to oldPage and return // false in the test function. nextPage++; } oldPage++; next(); }); }, function (err) { // All things are done! });