从node.js中的各种请求统一数据

发布于 2025-02-02 18:09:21 字数 1219 浏览 0 评论 0原文

在我在Node.js(初学者的位置)中的当前项目上,有一个可以查询的数据来源(Web API),但仅限于给定位置的一定数量的条目。不可能请求完整的记录集(例如1000个条目),您只能从偏移量中选择最多50个条目。

获取数据的功能如下:

let myResponseData = await client.getData({userId: myId, first: 50, after: cursor});

其中userId是身份验证,首先是我要请求的记录数(0-50),>是光标之后,

响应看起来像这样:

{
  "totalRecords": 1000,
  "page_info": {
    "has_next_page": true,
    "end_cursor": "this is some random string"
  },
  "data": [
    {
      "id:" 1,
      "name": "Name1"
    },
    {
      "id:" 2,
      "name": "Name2"
    }
  ]
}

我当前获取所有数据的进度是,我已经构建了一个函数,该函数从这样的数据源中读取所有

 let myResponseData = await client.getData({userId: myId});
 let hasNextPage = myResponseData.page_info.next_page;
 let cursor = myResponseData.page_info.cursor;
 
 while(hasNextPage){
  console.log('next...' + cursor);
  myResponseData= await client.getData({ userId: myId, first: 50, after: cursor});
  hasNextPage = myResponseData.page_info.next_page;
  cursor = myResponseData.page_info.cursor;
  myFunctionToJoinData(myResponseData); //Here i need help
}

页面下一个 +随机字符串。 我的目标是像请求中一样最终获得JSON数据对象,但是拥有所有查询的所有条目

如何实现?

On my current project in node.js (where I'm a beginner), there is a source of data that can be queried (WEB API) but is restricted to a certain amount of entries from a given position. It is not possible to request the full record set (eg. 1000 entries) you only can select a maximum of 50 Entries from an offset.

The function to get the data is as follow:

let myResponseData = await client.getData({userId: myId, first: 50, after: cursor});

where userId is an authentication, first is the number of records I want to request (0-50), and after is the cursor

the response looks like this:

{
  "totalRecords": 1000,
  "page_info": {
    "has_next_page": true,
    "end_cursor": "this is some random string"
  },
  "data": [
    {
      "id:" 1,
      "name": "Name1"
    },
    {
      "id:" 2,
      "name": "Name2"
    }
  ]
}

My current progress in fetching all data is, that i've already constructed a function that reads all pages from the data source like this:

 let myResponseData = await client.getData({userId: myId});
 let hasNextPage = myResponseData.page_info.next_page;
 let cursor = myResponseData.page_info.cursor;
 
 while(hasNextPage){
  console.log('next...' + cursor);
  myResponseData= await client.getData({ userId: myId, first: 50, after: cursor});
  hasNextPage = myResponseData.page_info.next_page;
  cursor = myResponseData.page_info.cursor;
  myFunctionToJoinData(myResponseData); //Here i need help
}

This works so far since the console logs the next + random string.
My goal is to end up with a json data object like in the request, but having all the entries from all queries

How can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文