How to get final object out of api call using airtable.js and node.js

I’m trying get all of the records form an api call into an object so I can use the data in other calculations. I can’t, however, get the function to return what I want. I’ve tried lots of combinations of async/await and .then(), but I can’t get it to work. “aTotalRecords” gets filled up with the data, but I can’t get the function to return it.

Can someone help me figure this out? Thanks in advance.

Here’s the code I have:

async function getAirtableData(){

    // var Airtable = require('airtable');
    var base = new Airtable({apiKey: oConfig.oConfig.airtableAPIKey}).base(oConfig.oConfig.airtableBaseKey);
    var aTotalRecords = []; // for keeping allthe records
    base('Polygon Transactions').select({
        
        maxRecords: 500
        // view: "Grid view"
    }).eachPage(function page(records, fetchNextPage) {
        // This function (`page`) will get called for each page of records.

        records.forEach(function(record) {
            aTotalRecords.push(record.get('tx_hash'));
            // console.log("length is: " + aTotalRecords.length);
            // console.log(record.get('tx_hash'));
        });
        // 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; }
    });

        // return Promise
    // console.log(aTotalRecords)
    // return await Promise.resolve(aTotalRecords)
    return aTotalRecords
    
};

// getAirtableData().then(console.log);

async function allAirtableRecords() {
    return getAirtableData();
};


allAirtableRecords().then(console.log);

Hey @David_Weinstein - have you tried the following?

var base = new Airtable({apiKey: oConfig.oConfig.airtableAPIKey}).base(oConfig.oConfig.airtableBaseKey);
var allRecords = await base('Polygon Transactions').select({ maxRecords: 500 }).all()

It looks like this was added in v0.5.0 and the changelog has another example here

This should work for you and be much simpler too.

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.