我使用twilio javascript sdk进行twilio可编程-聊天。
我想将分页应用于我的频道结果,但我无法弄清楚。
这是我目前的密码。
this.chatClient.getUserChannelDescriptors().then(paginator => {
// All channels are fetched
})我试图传递一个类似于pageSize工作方式的getMessages(10),但它没有工作。
this.chatClient.getUserChannelDescriptors(10).then(paginator => {
// The result was same, it fetched all the channels instead of just 10
})我正在寻找一个如何在频道上分页的例子。谢谢。
发布于 2019-08-09 14:16:37
我终于找到了办法。
应该递归地完成,因为我们通过调用getUserChannelDescriptors()获得初始列表,但是其余的记录可以通过调用nextPage()来获取;
async function processChannels(paginator) {
// Now, if hasNextPage is true
// call nextPage() to get the records instead of getUserChannelDescriptors()
if (paginator.hasNextPage) {
const nextPaginator = paginator.nextPage();
processChannels(nextPaginator);
} else {
console.log("END OF RECORDS");
}
}
async function getChannels() {
const paginator = await chatClient.getUserChannelDescriptors();
// Initiate the recursive function
if (paginator.items) {
await processChannels(paginator);
}
}这就是你会在每个电话里得到的。
发布于 2019-02-14 07:41:15
根据文档,getUserChannelDescriptors方法不带任何参数。
但是您不必手动分页,因为该方法返回一个Promise.<Paginator.<ChannelDescriptor>> type..which,意味着您应该能够访问twilio提供的分页功能。
您的paginator.items应该只有一个页面中的项目。
编辑:基本上关键是您的第一个片段是正确的。不幸的是,twilio不是开源的,所以我无法检查它们在哪里准确地定义了page_size。但是,我鼓励您创建100个模拟通道,然后检查paginator.items数组的大小。
试试这个:
this.chatClient.getUserChannelDescriptors().then(paginator => {
console.log(paginator.items, paginator.hasNextPage(), paginator.hasPrevPage());
})分页器类的文档是这里。
https://stackoverflow.com/questions/54685093
复制相似问题