API限制(200条记录)- 需要JavaScript计数器以获取所有记录

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

API limitation (200 Records)- Need javascript counter to get all records

问题

我从API中以JSON对象的形式获取记录,限制为200条。我需要获取800到1200条记录,取决于日期。

这种方式基本能用,但明显原因是它在每次迭代中都抓取相同的200条记录。我最终得到一个包含1200条记录的JSON对象,但1、201、401、601等记录都相同。
我觉得这里需要一个计数器,但我想不出如何实现。
感谢任何帮助或指引。
谢谢!

const iteration = new Array(0, 1, 2, 3, 4, 5);
const dealRecords = new Array();

for (var i = 0; i < iteration.length; i++) {
  SOMECOMPANY.API.searchRecord({
    Entity: "Greeting",
    Type: "criteria",
    Query: "(Text:equals:Hay)",
  }).then(function (data) {
    dealRecords.push({ data });
  });
}
console.log(dealRecords);
英文:

I'm getting records as a JSON object from an API with a limitation of 200 records. I need to get from 800 to 1200 records, depending on the day.

This (below) kind of works except, for obvious reasons, it's grabbing the same 200 records with each iteration. I end up with one JSON object with 1200 records but records 1, 201, 401, 601 etc. are all the same.
I feel like a counter is needed here but I can't figure out how to implement it.
Any help or pointed in the right direction is appreciated.
Thank you!

const iteration = new Array(0, 1, 2, 3, 4, 5);
const dealRecords = new Array();

for (var i = 0; i < iteration.length; i++) {
  SOMECOMPANY.API.searchRecord({
    Entity: "Greeting",
    Type: "criteria",
    Query: "(Text:equals:Hay)",
  }).then(function (data) {
    dealRecords.push({ data });
  });
}
console.log(dealRecords);

答案1

得分: 2

你需要使用偏移或分页参数来偏移获取的结果。具体的操作方式取决于你使用的API。例如,Twitter会提供一个分页令牌,而其他可能会提供一个查询参数来偏移结果。

英文:

You will need to offset the results you are getting by using an offset or pagination parameter (or anything similar).

The exact way of doing this will depend on the API you are using. Twitter for example provides you with a pagiantion token, while others might expose a query param to offset the results.

答案2

得分: 1

根据API文档,你可以在配置对象之后插入一个页面参数。我认为你不需要一个数组来进行循环。

示例代码:

for (var page = 1; page < 7; page++) {
    SOMECOMPANY.API.searchRecord({ Entity: "Greeting", Type: "criteria", Query: "(Text:equals:Hay)" }, page)
       ...
}

也许需要使用 page.toString(),因为页面参数必须是一个字符串。

或者你可以尝试更改 perPage 参数,只获取你所需的一页内容。

SOMECOMPANY.API.searchRecord({ Entity: "Greeting", Type: "criteria", Query: "(Text:equals:Hay)" }, "1", "1200")
英文:

According to the API doc you can insert a page param after the config object. And I think you won't need an array for the loop

Like:

for (var page = 1; page &lt; 7; page++) {
            SOMECOMPANY.API.searchRecord({ Entity: &quot;Greeting&quot;, Type: &quot;criteria&quot;, Query: &quot;(Text:equals:Hay)&quot; }, page)
               ...
        }

perhaps with page.toString() because the page param has to be a string.

Or you can try to change the perPage param and get just one page with all you need.

SOMECOMPANY.API.searchRecord({ Entity: &quot;Greeting&quot;, Type: &quot;criteria&quot;, Query: &quot;(Text:equals:Hay)&quot; }, &quot;1&quot;, &quot;1200&quot;)
`` 

</details>



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

发表评论

匿名网友

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

确定