我正在使用 zomato API,它在一个 api 调用中只发送 20 个名称,我想要至少 60 个响应,所以我想调用相同的 api 3 次并将所有响应组合在一起。
app.get('/locations/:query', async (req, res) => {
const query = req.params.query;
const data = await zomato.cities({ q: query,count: 1 })
const cityId= await (data[0].id);
const restaurants1 = await zomato.search({ entity_id: cityId, entity_type: 'city', start:0, count:20, sort:'rating', order:'desc' })
const restaurants2 = await zomato.search({ entity_id: cityId, entity_type: 'city', start:20, count:40, sort:'rating', order:'desc' })
const restaurants = Object.assign(restaurants1,...restaurants2);
res.send(restaurants);
})
截至目前,我只尝试到 40 岁,但即使这样也行不通。如果存在另一个常量restaurant3
,它的起始值为 40,结束值为 60,我该如何合并这三个并将它们发回?
我之前没有使用过 zomato-api 但根据他们的API-documentation,你应该能够做到这一点:
app.get('/locations/:query', async (req, res) => {
const query = req.params.query;
const data = await zomato.cities({ q: query,count: 1 })
const cityId= data[0].id;
const result = [];
const nrOfRequests = 2; // change this accordingly to increase the nr of requests
let currCount = 0;
const nrOfEntries = 20;
for(let i=0; i < nrOfRequests ; i++) {
const response = await zomato.search({ entity_id: cityId, entity_type: 'city', start:currCount, count:nrOfEntries, sort:'rating', order:'desc' });
result.push(...response.restaurants);
currCount += nrOfEntries;
}
res.send(result);
});
基本上,您应该能够通过更新start
每次迭代的参数然后将restaurants
结果存储在结果中来循环所需数量的请求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句