1

I'm trying to exports the results of my Airtable records into a json file.

I'm using a react-js env in codesandboxio.

I coded a function that retrieve all the records, but i can only console.log their value.

function loadGeneralItems(table) { 
var recList = []
    base(table).select().all()
    .then(
       function (records)  { 
        for (let i=0; i< records.length; i++)
       recList.push(records[i]._rawJson.fields) 
      // console.log(recList) // outputs ([Object, ...])
      }); 
   // return recList // returns nothing         
}

I would like to return recList and parse it as a json object.

assayag.org
  • 709
  • 10
  • 24

1 Answers1

1

Solution was to transform the function into async.

Here is the function:

var loadGeneralItems = async function loadGeneralItems(table) {
var recList = []
    await base(table).select().all()
    .then(
       (records) => {
        for (let i=0; i< records.length; i++)

       recList.push(records[i]._rawJson.fields)})

    .catch(function () {
      console.log("Promise Rejected");
});
      return recList
}

and here is the call :

app.get('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});

  data = loadGeneralItems("Works")

  var dataPromise = Promise.resolve(data);
  dataPromise.then(function(jsonData) {
  res.write(JSON.stringify(jsonData));

  });
assayag.org
  • 709
  • 10
  • 24