Variable amount of requests (hasNext)

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?

You could use a Request node to get the first one and pass it to a Join node configured as

Also use a Switch node to test whether this is the last one and if not then feed that back round to the request node again to get the next one. When the last one has been received then send msg.complete to the Join node and it will release the array.

1 Like