Created
September 5, 2024 18:40
-
-
Save itsmacr8/415adae3b6fc16bb582d2a11549827ea to your computer and use it in GitHub Desktop.
FetchNextPage not working properly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Airtable, { Base } from 'airtable'; | |
import { DatabaseRecord } from "../../types/DatabaseRecord.interface"; | |
class AirTable { | |
private apiKey: string = import.meta.env.VITE_PWK; | |
private apiBase: string = import.meta.env.VITE_PWB; | |
public base: Base = this.getBase(this.apiKey, this.apiBase); | |
private dbRecords: DatabaseRecord[] = []; | |
private fetchNext!: () => void; | |
getBase(apiKey: string, apiBase: string): Base { | |
return new Airtable({ apiKey: apiKey }).base(apiBase); | |
} | |
async getRecords() { | |
const dbRecords: DatabaseRecord[] = []; | |
try { | |
const records = await this.base('Projects') | |
.select({ | |
maxRecords: 12, | |
view: 'Grid view', | |
}) | |
.firstPage(); | |
records.forEach((record) => { | |
dbRecords.push(record.fields); | |
}); | |
return dbRecords; | |
} catch (err) { | |
console.error(err); | |
throw err; | |
} | |
} | |
async getRecords2() { | |
this.base('Projects') | |
.select({ pageSize: 12, view: 'Grid view' }) | |
.eachPage( | |
(records, fetchNextPage) => { | |
records.forEach((record) => { | |
this.dbRecords.push(record.fields); | |
}); | |
this.fetchNext = fetchNextPage; | |
} | |
); | |
return this.dbRecords; | |
} | |
async getRecords3(): Promise<DatabaseRecord[]> { | |
const dbRecords: DatabaseRecord[] = []; | |
try { | |
await this.base('Projects') | |
.select({ pageSize: 12, view: 'Grid view' }) | |
.eachPage(async (records, fetchNextPage) => { | |
records.forEach((record) => { | |
dbRecords.push(record.fields); | |
}); | |
this.fetchNext = fetchNextPage; | |
}); | |
} catch (error) { | |
console.error('Error fetching records:', error); | |
throw error; | |
} | |
return dbRecords; | |
} | |
} | |
const db = new AirTable() | |
console.log(await db.getRecords()) | |
console.log(await db.getRecords2()) | |
console.log(await db.getRecords3()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment