我无法使用 pageNumber 和 pageSize 获取消息列表。 它每次都会返回完整的结果。 有没有办法通过pageSize和pageNumber查询数据。
twilioClient.chat.services(TWILIO_CHAT_SERVICE_SID)
.channels(req.params.id)
.messages
.list({
pageSize: 2,
page: 2
}, (error, messages) => {
if (error) {
return res.status(error.status).json({
message: 'Problem in getting messages',
error: error
});
}
if (Utility.isEmptyObject(messages)) {
return res.status(404).send('Messages not found');
}
res.status(200).json({
message: 'Messages retrieved sucessfully',
docs: messages
});
});
我想通过pageSize和pageNumber查询结果。同时,如果我发送 REST API 调用,它会返回带有 pageSize 的结果,但 pageNumber 在这里也不起作用。
let limitCount = parseInt(req.query.limit > 0 ? req.query.limit : 10); // limitValue
let pageNo = parseInt(req.query.pageNo > 0 ? req.query.pageNo : 0);
let options = {
method: 'GET',
url: 'https://chat.twilio.com/v2/Services/IS/Channels/' +
'CH2f/Messages?Page=' + pageNo + '&PageSize=' + limitCount,
headers:
{
'cache-control': 'no-cache',
Authorization: 'Token'
}
};
request(options, function (error, response, body) {
console.log(body);
if (error) {
//console.error("There was an error loading the channels.", error);
return res.status(error.status).json({
message: 'Problem in getting messages',
error: error
});
}
if (Utility.isEmptyObject(response)) {
return res.status(404).send('Messages not found');
}
res.status(200).json({
status: 'Messages retrieved sucessfully',
messages: JSON.parse(body)
});
});
非常感谢任何帮助。
最佳答案
这里是 Twilio 开发者布道者。
Twilio 资源不支持基于页码的分页。相反,数据页将包含 next_page_uri
和 previous_page_uri
属性。您可以使用 PageSize
参数控制页面大小。
使用库时,您可以让它们自动分页。例如:
twilioClient.chat.services(TWILIO_CHAT_SERVICE_SID)
.channels(req.params.id)
.messages
.list({
limit: 400,
pageSize: 100
});
最终将调用 API 4 次以返回总共 400 条消息。
您可以阅读有关 pagination of Twilio resources in the docs. 的更多信息
关于node.js - Twilio Chat API - 获取带有分页的消息列表(pageNo,pageLimit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54252305/