Help

How to use return outside of select method

Topic Labels: Formulas Integrations
512 1
cancel
Showing results for 
Search instead for 
Did you mean: 
cburrell1896
4 - Data Explorer
4 - Data Explorer

Hello, I am using the Airtable API in javascript. Currently I am using the select method based on the documentation, and I am trying to make use of the returned data outside of the method.

For example this is my code:

 

 

base('TestData').select({
    view: 'Grid view',
    filterByFormula: `{FUS} = "F002"`
}).firstPage(function (err, records) {
    if (err) { console.error(err); return; }
    records.forEach(function (record) {
        PAISearch = record.get("PAISearch");
    });
});
console.log(PAISearch);

 

 

 

When I try to make use of the PAISearch variable that gets declared in the return of the select method, I can't get it to be accessible under the Global scope.

I would like some advice on how to use the select method to store data into global variables for use in other functions. This works great if I just want to log to console inside of the function, but I need to make use of them elsewhere.

I understand this is probably some basic JS fundamentals, as I am still learning. Thanks!

1 Reply 1
Clasicwebtools
5 - Automation Enthusiast
5 - Automation Enthusiast

base('TestData').select() method is asynchronous, meaning that it doesn't pause the execution of the rest of the script while it's waiting for a response. The console.log(PAISearch) line executes before the callback function provided to .firstPage() has a chance to assign a value to PAISearch. Hence, PAISearch is undefined when trying to log it to the console.

You can cehck the JS docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function