# Variable amount of requests (hasNext)

**URL:** <https://discourse.nodered.org/t/variable-amount-of-requests-hasnext/95806>\
**Category:** General\
**Created:** [5 March 2025 13:55 UTC](https://discourse.nodered.org/t/variable-amount-of-requests-hasnext/95806 "2025-03-05T13:55:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [5 March 2025 13:55 UTC](https://discourse.nodered.org/t/variable-amount-of-requests-hasnext/95806/1 "2025-03-05T13:55:45Z")

</div>

I need some help figuring out a clean way to handle variable number of http requests. Working with thingsboard API, they use pagination:

```javascript
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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 March 2025 15:51 UTC](https://discourse.nodered.org/t/variable-amount-of-requests-hasnext/95806/2 "2025-03-05T15:51:26Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20acc3abfcccd7ae4464fd51abf0830b3d17c9b4.png)

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 June 2025 15:52 UTC](https://discourse.nodered.org/t/variable-amount-of-requests-hasnext/95806/3 "2025-06-03T15:52:23Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
