【发布时间】:2021-11-26 09:04:15
【问题描述】:
我正在从每次调用限制为 50 条记录的 API 获取数据。我总共需要检索 10,000 条记录,因此通过 offset 参数使用分页来实现这一点。
我不太熟悉需要使用的语法,但我想继续循环获取,直到单个调用返回的记录数
除了给我 10,000 的最终“计数”之外,我还需要将每次调用的 JSON 响应附加在一起
let offset = 0
fetch(`https://api.opensea.io/api/v1/assets?collection=womenandweapons&format=json&offset=${offset}&limit=50&order_direction=desc`,
{
method: 'GET',
headers: {
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(data => {
let len = Object.keys(data.assets).length
console.log(len)
console.log(data.assets)
})
offset += 50
【问题讨论】:
-
limit=${offset}&limit=50应该类似于offset=${offset}&limit=50。 -
如果您使用 async/await(在
fetch上),您可以简单地使用for或while循环。
标签: javascript fetch