Search code examples
javascriptnode.jsapiairtable

How to get async await work in airtable api with node js to get all records in the airtable before sending resolve?


I am trying to get my record from airtable using async await. What i want is that once airtable has fetched all the records it should return a value 1 which will be assigned to a variable counter. Once value of counter is 1 it can be used to access other data as well. The problem is that since airtable api has a record limit of 1. Even before it reaches all the elements it sets the value of counter to 1. I know this is weird because page function should be called in airtable api before done function unless it has traversed across all the records. Here is my code :

var counter = 0;

async function FirstCall(){
    counter = await backlinksubmissionChecker.getData();
    console.log("Counter value is ",counter);
    if(counter==1){
        counter = await codequalityXP.getData(); 
        console.log("Counter value is ",counter);
    }
} 

my code for airtable api access is like this

async function getData()
{
    return new Promise((resolve, reject) => {
            console.log("Inside Backlink submission checker");
            var i=0;
            let value =  base("Program XP").select({      
            }).eachPage(function page(records, fetchNextPage) {   
                records.forEach(function(record) {
                    let recordID =  record.get("RecordID");
                    let email = record.get("Email Address");
                    ConnectStudents(recordID,email);
                 i++;
                });
                fetchNextPage();
            }, function done(err) {
                if (err) { 
                console.error(err);
                reject("Promise rejected");
                }else{
                resolve(1);
            }
            }); 
      });
}

How do I ensure that my airtable api has brought me all the records before it sends a resolve promise which tells that it has traversed through all records.


Solution

  • It looks like you are using Airtable's NodeJS SDK. There's a built in function for selecting all records which I think simplifies your code greatly and decreases the risk of other errors.

    async function getData() {
      console.log("Inside Backlink submission checker");
      const records = await base('Program XP').select().all();
    
      var i = 0;
      records.forEach(function(record) {
          let recordID =  record.get("RecordID");
          let email = record.get("Email Address");
          ConnectStudents(recordID,email);
          i++;
      });
    
      return 1;
    }
    
    getData()
      .then((res)=>console.log("DONE: ", res))
      .catch((err)=>{console.log("ERR: ", err)});
    

    The problem is that since airtable api has a record limit of 1

    The GET method for Airtable's API returns 1 page at a time with a default of 100 records per page. If you have less than 100 records in the table you are making the GET request to, then it will return all records in a single call.

    If your ConnectStudents function is asynchronous, it may not be fully processing each record, but it does look like the Airtable API is returning all of the records to your system.