答案 0 :(得分:4)
从您的文档链接中,我假设您正在使用REST API。在REST API中,有一个pageToken
参数,您可以指定该参数。这可以从上一个请求返回的nextPageToken
派生。
{
"documents": [
{
object(Document)
}
],
"nextPageToken": ABCDEF1234567890,
}
projects/my-project/databases/my-database/documents or projects/my-project/databases/my-database/documents/chatrooms?pageSize=20&pageToken=ABCDEF1234567890
答案 1 :(得分:2)
Firestore范围查询基于具有锚文档。因此,您必须知道范围开头的文档(按顺序排列的属性),然后使用ref.startAfter(anchorDdoc).limit(10)
(或ref.startAfter(valueOfAnchorDoc).limit(10)
)来获取下一页。
Firestore服务器端Admin SDKs have an offset()
call,允许服务器确定要启动的文档。但是客户端SDK没有这种方法。
答案 2 :(得分:0)
这可能有助于在没有页码的情况下进行分页(上一页和下一页):
app.get('/page/next/:startAt',(req, res)=>{
(async ()=>{
try{
const startAt = req.params.startAt;
let query = db.collection('movies');
let response = [];
console.log(query);
await query.orderBy('time', 'desc').where("time", "<", new Date(parseInt(startAt)*1000)).limit(16).get().then(snapshots => {
let docs = snapshots.docs;
for (let doc of docs){
console.log(doc.data());
const selectedItems = {
id: doc.id,
name: doc.data().name,
desp: doc.data().desp,
site: doc.data().site,
time: doc.data().time
}
response.push(selectedItems);
}
return response; //each then should return a vlue
})
return res.status(200).send(response);
}catch (error){
console.log(error);
return res.status(500).send(error);
}
})();
});
app.get('/page/back/:backAt',(req, res)=>{
(async ()=>{
try{
const backAt = req.params.backAt;
let query = db.collection('movies');
let response = [];
console.log(query);
await query.orderBy('time', 'desc').where("time", ">", new Date(parseInt(backAt)*1000)).limit(16).get().then(snapshots => {
let docs = snapshots.docs;
for (let doc of docs){
console.log(doc.data());
const selectedItems = {
id: doc.id,
name: doc.data().name,
desp: doc.data().desp,
site: doc.data().site,
time: doc.data().time
}
response.push(selectedItems);
}
return response; //each then should return a vlue
})
return res.status(200).send(response);
}catch (error){
console.log(error);
return res.status(500).send(error);
}
})();
});