gpt4 book ai didi

javascript - 如何获取 API 响应中多个页面的结果?

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:37 25 4
gpt4 key购买 nike

我想对特定 ChartMogul Customers.All() API 调用的所有结果进行分页。我正在使用Chartmogul NPM package .

它有效,我可以获取所有结果,但是我的结果多于单次调用中允许的最大记录数,因此我需要翻阅多页结果。我不知道如何优雅地做到这一点。

我可以执行多个调用,首先获取页面总数,然后循环遍历所有页面。将所有结果保存在一个大对象中。

这是调用,page 变量应更新为 x 页。

ChartMogul.Customer.all(config, {
status: 'Active',
per_page: 200,
page: page
}, function (err, res) {
if (err) reject(err);
console.log(res);
resolve(res.entries);
});

chartmogul 的每个 API 响应都包含以下详细信息

{
"entries":[
{
"id": 25647,
"uuid": "cus_de305d54-75b4-431b-adb2-eb6b9e546012",
"external_id": "34916129",
"external_ids": ["34916129"],
"data_source_uuid": "ds_610b7a84-c50f-11e6-8aab-97d6db98913a",
"data_source_uuids": ["ds_610b7a84-c50f-11e6-8aab-97d6db98913a"],
"name": "Example Company",
"company": "",
"email": "bob@examplecompany.com",
"status": "Active",
"lead_created_at": "2015-01-01T10:00:00-04:00",
"free_trial_started_at": "2015-01-09T10:00:00-04:00",
"customer-since": "2015-06-09T13:16:00-04:00",
"city": "Nowhereville",
"state": "Alaska",
"country": "US",
"zip": "0185128",
"attributes":{
"tags": ["engage", "unit loss", "discountable"],
"stripe":{
"uid": 7,
"coupon": true
},
"clearbit":{
"company":{
"name": "Example Company",
"legalName": "Example Company Inc.",
"domain": "examplecompany.com",
"url": "http://examplecompany.com",
"category":{
"sector": "Information Technology",
"industryGroup": "Software and Services",
"industry": "Software",
"subIndustry": "Application Software"
},
"metrics":{
"raised": 1502450000,
"employees": 1000,
"googleRank": 7,
"alexaGlobalRank": 2319,
"marketCap": null
},
},
"person":{
"name":{
"fullName": "Bob Kramer"
},
"employment":{
"name": "Example Company"
}
}
},
"custom":{
"CAC": 213,
"utmCampaign": "social media 1",
"convertedAt": "2015-09-08 00:00:00",
"pro": false,
"salesRep": "Gabi"
}
},
"address":{
"address_zip": "0185128",
"city": "Nowhereville",
"country": "US",
"state": "Alaska"
},
"mrr": 3000,
"arr": 36000,
"billing-system-url": "https:\/\/dashboard.stripe.com\/customers\/cus_4Z2ZpyJFuQ0XMb",
"chartmogul-url": "https:\/\/app.chartmogul.com\/#customers\/25647-Example_Company",
"billing-system-type": "Stripe",
"currency": "USD",
"currency-sign": "$"
},
{"...49 more...": "...entries..."}
],
"has_more": true,
"per_page": 50,
"page": 1,
"current_page": 1,
"total_pages": 4
}

那么如何以简单、优雅的方式循环遍历响应的所有页面?

最佳答案

这种模式应该能让你顺利上路。

使用返回主要 Promise 的函数,当有更多 Promise 时,您返回对该函数的递归调用(另一个 Promise),或者返回组合结果。

以下内容未经测试,从未使用过此 API 可能需要一些调整。

function getEntries(config, page=0, entries = []) {
// return promise
return ChartMogul.Customer.all(config, {
status: 'Active',
per_page: 200,
page: page
}).then(res => {
// merge into main array
entries = [...entries, ...res.entries];
// return another promise if there are more ... or return the combined array
return res.has_more ? getEntries(config, res.page+1, entries) : entries;
});

}

getEntries(config)
.then(entries => console.log(entries) )
.catch(err => console.error(err) )

关于javascript - 如何获取 API 响应中多个页面的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56958945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com