I need some help figuring out a clean way to handle variable number of http requests. Working with thingsboard API, they use pagination:
async getMainElectricMeterDevices() {
const mainElectricMeterProfileId = "abc";
let hasNext = true;
let page = 0;
const devices = [];
while(hasNext) {
const url = `https://thingsboard.cloud/api/deviceInfos/all?pageSize=100&page=${page}&includeCustomers=true&deviceProfileId=${mainElectricMeterProfileId }`;
const response = await scope.fetchRequest("GET", url);
devices.push.apply(devices, response.data);
hasNext = response.hasNext;
page++;
}
return devices;
}
I want to run this request for as long as hasNext
is true (and increment page
). This is examplified in a simple while loop in the code above.
How to do this in node red and at the same time get all data collected in the same object (msg)? Any easy or clean way I'm not aware of?