I’m pretty new to Node.js and trying to get better at it. I have an Airtable database and I need to fetch all records from a specific table. The Airtable Node.js API works fine for getting the data, but I want to store all these records in an array so I can export them later as JSON or Excel files.
Since the API calls are asynchronous, I’m using callbacks but I think I might be doing it wrong. I keep hearing about Promises as a better solution but I’m still learning how they work.
Here’s what I have so far:
var AirtableLib = require('airtable');
AirtableLib.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: 'your-api-key-here'
});
var database = AirtableLib.base('your-base-id');
var viewName = "Grid view";
var dataArray = [];
database('Products').select({
view: viewName
}).eachPage(function handlePage(recordList, getNext) {
recordList.forEach(function(item) {
dataArray.push({
"Title": item.get('Title'),
"Description": item.get('Description')
});
});
getNext();
processArray(dataArray);
}, function finished(err) {
if (err) {
console.log(err);
console.log(dataArray);
}
});
function processArray(dataArray) {
convertToJson(dataArray);
return dataArray;
}
function convertToJson(dataArray) {
console.log(dataArray);
return JSON.stringify(dataArray);
}
Should I switch to using Promises here? I’m worried about creating too many callback functions and making the code messy. Any advice on the best approach?