[英]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.