Skip to main content

How to use return outside of select method

  • April 9, 2024
  • 1 reply
  • 0 views

Forum|alt.badge.img+1

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

Forum|alt.badge.img+3
  • Participating Frequently
  • 8 replies
  • April 14, 2024

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


Reply