Home > Mobile >  How to save JSON-Response from multiple API-Requests?
How to save JSON-Response from multiple API-Requests?

Time:12-09

I have many Ids from a API-Request:

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

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]'

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?

CodePudding user response:

It's not clear if this.get returns a string, object or array. Given this, the easiest solution is to make userStories an array and push the results of each call onto it. 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)
  • Related