Help

Running Javascript function when all 100+ records have been retrieved?

Topic Labels: API
2490 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Per_Carlen
7 - App Architect
7 - App Architect

Below is the example code from https://airtable.com/api for listing records with Javascript. How should I adjust the code when my base have more than 100 records, and I want to run some code when all the records have been retrieved?

(I know that I’ll have to remove “maxRecords: 3,”)

If I put my code within “eachPage” but then the code will be executed multiple times (obviously).

I’ve tried to put my code in inside an else statement within the done-function but then I don’t have access to the variables and methods like records, record, record.get and so on.

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('appInTsFatECjqsT2');

base('Uppställningsplatser').select({
    // Selecting the first 3 records in Alla uppställningsplatser:
    maxRecords: 3,
    view: "Alla uppställningsplatser"
}).eachPage(function page(records, fetchNextPage) {
    // This function (`page`) will get called for each page of records.

    records.forEach(function(record) {
        console.log('Retrieved', record.get('Uppställningsplats'));
    });

    // To fetch the next page of records, call `fetchNextPage`.
    // If there are more records, `page` will get called again.
    // If there are no more records, `done` will get called.
    fetchNextPage();

}, function done(err) {
    if (err) { console.error(err); return; }
});
1 Reply 1
Julian_Wan
5 - Automation Enthusiast
5 - Automation Enthusiast

Saw this when I was looking for the same solution.

It was frustrating, but I found the simplest answer…

Airtable secretly added an .all() method which has been greatly under-reported.

For example, I have a base with a table called ‘Contacts’ and this will let me pull all and handle each:

 const records = await base('Contacts').select().all()

  console.log(records.length) // logs the quantity of records you've pulled

  for (const record of records) {
    console.log(record.id) // logs the record id
    const name = record.get('name') // lets you grab and handle value
    console.log(name)
    // do other things, it's your life, whatever...
  }

This was released officially on on Apr 29, 2017:

The “official” method is:

table.select({view: 'Main View').all().then(records => {
    // records array will contain every record in Main View.
}).catch(err => {
    // Handle error.
})

Hope this helps the next person. I spent hours looking for this obscure answer.

“WHY IS THIS NOT DOCUMENTED ON THE API PAGE” he shouted angrily, to an empty room.