简体   繁体   English

如何从多个 API 请求中保存 JSON 响应?

[英]How to save JSON-Response from multiple API-Requests?

I have many Ids from a API-Request:我有很多来自 API 请求的 ID:

const data = { query: "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'User Story' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc" };
    const list = await this.post<any>('wit/wiql', data);
    let ids: string[] = [];
    for (let i = 0; i < list.data.workItems.length; i++) {
      ids.push(list.data.workItems[i].id);
    }

With this ids I have to do a API-Request to another endpoint but this endpoint can only take 200 Ids in one request see https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work-items/list?view=azure-devops-rest-7.0&tabs=HTTP使用此 ID,我必须对另一个端点执行 API 请求,但此端点在一个请求中只能接受 200 个 ID,请参阅https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/工作项目/列表?view=azure-devops-rest-7.0&tabs=HTTP

Now I try to make multiple requests and save the results like this:现在我尝试发出多个请求并像这样保存结果:

let ids2: string[] = [];
    let userStories;
    for (const id of ids) {
      if (ids2.length == 200) {
        userStories += await this.get<any>(`wit/workitems?ids=${ids2}`);
        ids2.splice(0);
      }
      ids2.push(id);
    }
    if (ids2.length > 0) {
      userStories += await this.get<any>(`wit/workitems?ids=${ids2}`);
    }

But userStories is 'undefined[object Object][object Object]'但是userStories'undefined[object Object][object Object]'

I tried to save the data in multiple ways but it did not work.我尝试以多种方式保存数据,但没有用。 How can I save the data and work with it later easily?如何保存数据并在以后轻松使用它?

It's not clear if this.get returns a string, object or array.目前尚不清楚 this.get 返回的是字符串 object 还是数组。 Given this, the easiest solution is to make userStories an array and push the results of each call onto it.鉴于此,最简单的解决方案是使 userStories 成为一个数组并将每次调用的结果推送到它上面。 Then you can do what you like with the results.然后你可以对结果做你喜欢的事。

const userStories = []
for (const id of ids) {
  if (ids2.length === 200) {
    userStories.push(await this.get<any>(`wit/workitems?ids=${ids2}`))
    ids2.splice(0)
  }
  ids2.push(id)
}
if (ids2.length > 0) {
  userStories.push(await this.get<any>(`wit/workitems?ids=${ids2}`))
}

console.log('Number of userStories is', userStories.length)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在RxJS中处理此json-response? - How to process this json-response in RxJS? 如何将HTTP JSON响应转换为HTML(Ionic2,Angular2,TypeScript,PHP,JSON)? - How to get HTTP JSON-response into HTML (Ionic2, Angular2, TypeScript, PHP, JSON)? 如何在angular 2应用程序中从http json响应保存访问令牌? - How to save an access Token from a http json response in angular 2 app? 如何使用 Angular 从 REST API 读取 JSON 响应 - How to read JSON response from a REST API using Angular 如何解决 api 服务器对 angular/typescript 中的 http 请求的响应延迟? - How to account for delay in response from api server on http requests in angular/typescript? 使用来自 json 响应的数据递归发送 http 请求 - Recursively sending http requests using data from json response 没有收到来自 db 的响应,API 在没有发送 /api/dates 的响应的情况下解析,这可能会导致请求停止 - NextJS - Not receiving response from db, API resolved without sending a response for /api/dates, this may result in stalled requests - NextJS 多个Api请求 - Multiple Api requests 将 API 中的 ByteArray 响应转换为 Typescript 中的文件并保存 - Convert ByteArray response from API to a File in Typescript and save it 如何从我正在使用angular6的api访问JSON数据响应 - how to access the JSON data response from the api I am using angular6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM