Thought I would post this here for teh community given that it involved quite a bit of head scratching.
The context is that I’m using HubDB data as a source to auto-suggest and validate a specific form field via a var list[]. Life was relatively simple until I reached the single API call limit of 1000 data records. At that point I needed a rethink, to upgrade to V3 API, and learn JS promises and async functions.
Below is the code that I came up with and is now working - maybe it can be improved?
I’ll post the overall auto-suggest approach elsewhere as it builds significantly on the base form customization ideas at How to customize the form embed code (hubspot.com)
Steve
var list = [];
var hubdbURL ="your HubDB API reference URL without credentials";
// Cunning async promise await stuff
async function getHubdbPage(aURL) {
let after ="";
console.log("This URL: ", aURL)
let pagePromise = new Promise(function(resolve,reject) {
let req = new XMLHttpRequest();
let afterThis = "";
req.open('GET', aURL);
req.onload = function() {
if (req.status != 200 ) {
console.log("Error: ", this.response);
reject("Fatal HubDB error");
}
else {
// Success!
var data = JSON.parse(this.response);
// V3 API structure
var tableArr = data.results;
if (data.total > tableArr.length+list.length ) {
afterThis = data.paging.next.after;
}
console.log("Page here: ", this.status, data.total, afterThis);
//console.log("Len, Row[0]: ", tableArr.length, tableArr[0].values);
tableArr.forEach(function(obj) {
// console.log(obj.values);
if(obj.values["name"] != null) {
var name = obj.values["name"];
// push to names array
list.push(name);
}
});
// resolve promise with indicator of need for another call
resolve(afterThis);
}
};
req.send();
});
//console.log("Before resolve: ", after);
after = await pagePromise;
//console.log("After resolve: ", after);
return(after);
}
// need to get this into async loop somehow...
async function getAllRecords() {
try {
// if no error we see if there is data remaining and then call again
after = await getHubdbPage(hubdbURL);
console.log("1st async call: ", list.length, after);
while (after.length > 0) {
let afterURL = hubdbURL + "&after=" + after;
after = await getHubdbPage(afterURL);
console.log("Follow-on async call: ", list.length, after);
}
} catch(error) {
console.log(error);
}
}
// lets do it!
getAllRecords();