Problems with the Pagination using HTTPS in Node.JS

Heres what I do in plain JS (which would carry over to nodeJS

        var limit = 50;
        function fetchStreams(game_id, page, after, tot) {
            page = page ? page : 0;
            document.getElementById('loader_' + game_id).textContent = 'Loading Page: ' + page + ' Current ' + tot;
            fetch(
                'https://api.twitch.tv/helix/streams?first=100&game_id=' + game_id + (after ? '&after=' + after : ''),
                {
                    "headers": {
                        "Client-ID": client_id,
                        "Authorization": "Bearer " + access_token
                    }
                }
            )
            .then(resp => resp.json())
            .then(resp => {
                document.getElementById('loader_' + game_id).textContent = 'Processing Page: ' + page;

                var total = parseInt(document.getElementById('count_' + game_id).textContent);
                for (var x=0;x<resp.data.length;x++) {
                    total += resp.data[x].viewer_count;
                }
                document.getElementById('count_' + game_id).textContent = total;

                var d = document.createElement('td');
                d.textContent = resp.data.length;
                document.getElementById('loader_row_' + game_id).append(d);

                // loop if we got a cursor
                if (resp.hasOwnProperty('pagination') && resp.pagination.hasOwnProperty('cursor')) {
                    page++;
                    // do a page limit check
                    if (page >= limit) {
                        document.getElementById('loader_' + game_id).textContent = 'Stopped at Page: ' + page + ' - ' + resp.data.length;
                        return;
                    }
                    fetchStreams(game_id, page, resp.pagination.cursor, total);
                } else {
                    document.getElementById('loader_' + game_id).textContent = 'Last Page: ' + page + ' - ' + resp.data.length;
                }
            })
            .catch(err => {
                console.log(err);
                document.getElementById('loading').textContent = 'Something went wrong';
            });
        }
  • Call fetchStreams(game_id)
  • If response contains a cursor then call fetchStreams(game_id, theCursor)
  • if response contains no cursor (or hit max page limiter - I stop at 50 here) call a completion function

This example exists as a self runnable browser example

https://barrycarlyon.github.io/twitch_misc/examples/browse_categories/