I’m working on a Next.js project and need help with async loops when fetching data from multiple Airtable databases. Right now I can pull data from one database successfully, but I want to loop through several database IDs and combine all the results.
I have an array of database identifiers like ["db123", "db456", "db789"]
and want to fetch records from each one. Here’s my current working code for a single database:
const data = await airtable
.base("db123")("Company Info")
.select()
.firstPage();
const companies = data.map((company) => {
return {
location: company.get("Location") || null,
title: company.get("Title") || null,
region: company.get("Region") || null,
};
});
return {
props: {
companies,
},
};
I tried manually creating separate requests but this approach doesn’t scale well:
export async function getStaticProps() {
const firstBatch = await getCompanyData("db123")
.select({})
.firstPage();
const secondBatch = await getCompanyData("db456")
.select({})
.firstPage();
const allData = [];
allData.push(processCompanyRecords(firstBatch));
allData.push(processCompanyRecords(secondBatch));
return {
props: {
allData,
},
};
}
The getCompanyData
helper function connects to Airtable:
const getCompanyData = (dbId) =>
base.base(dbId)("Company Info");
How can I properly loop through multiple database IDs and await each request before combining the results?