Twilio Chat getChannelDescriptors() paginator page items undefined

48 views Asked by At

Recently ran into an anomaly that we couldn't figure out to the end but had to program in a failsafe.

The problem is that the Paginator returned by the Client.getUserChannelDescriptors() has next pages, but page.items returns undefined and the following while loop will remain in infinitive loop:

async getAllRoomDescriptors() {
    try {

        var descPaginator = await TwilioClient.getChannelDescriptors()
        var descs = descPaginator.items.concat([])
        
        console.log('descPaginator: ', descPaginator); // Paginator looks normal

        // NB! This is infinitive loop!
        while (descPaginator.hasNextPage) {
            var pag = descPaginator.nextPage()     // returns another Paginator (page)
            console.log('pag.items: ', pag.items); // returns undefined!!!
            descs = descs.concat(pag.items)
        }
        return descs
    } catch (error) {
        throw new Error(error)
    }
}

In order to solve this infinitive loop and undefined page items I added the following failsafes (read hacks) to get it working without understanding the root cause of the issue.

async getAllRoomDescriptors() {
    try {
        var descPaginator = await TwilioClient.getChannelDescriptors()
        var descs = descPaginator.items.concat([])
        var currPage = 0
        var maxAllowedPages = 60

        while (descPaginator.hasNextPage) {
            currPage++
            var pag = descPaginator.nextPage()

            // 1st sign of trouble if items are undefined
            if(!pag.items) break;

            descs = descs.concat(pag.items)

            // 2nd problem, infintity loop avoidance check
            if(currPage >= maxAllowedPages) break;
        }
        return descs
    } catch (error) {
        throw new Error(error)
    }
}

All Changelogs from 3.3.5 up to 4.0.0 don't mention anything about it and the documentation on how the Paginator work hasn't changed either.

Does anyone know what might be causing this issue. Same behavior on both 3.3.5 and 4.0.0. Also this seems to be isolated to specific user as I couldn't recreate this in another used which much less channels. This happened to the most "loaded" Twilio user.

1

There are 1 answers

0
Marius Ilie On

You have an error in your code: descPaginator.nextPage() returns a Promise so you have to use await again. Something like this:

while (descPaginator.hasNextPage) {
  descPaginator = await descPaginator.nextPage();
  //do stuff
}