【问题标题】:Pagination in firestore apiFirestore api中的分页
【发布时间】:2018-02-20 11:55:57
【问题描述】:

我正在尝试使用 firestore beta 对文档进行分页。我在关注官方docs

我看到有一个 pageSize 参数来说明您要显示多少个文档,但我没有看到任何偏移选项。有人知道我怎么做分页系统吗?

谢谢

【问题讨论】:

标签: firebase google-cloud-firestore


【解决方案1】:

从您的文档链接中,我假设您使用的是 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

【讨论】:

    【解决方案2】:

    Firestore 范围查询基于具有锚点的文档。所以你必须知道范围开始的文档(order-by properties),然后使用ref.startAfter(anchorDdoc).limit(10)(或ref.startAfter(valueOfAnchorDoc).limit(10))获取下一页。

    Firestore 服务器端Admin SDKs have an offset() call,允许服务器确定要从哪个文档开始。但是客户端SDK没有这个方法。

    【讨论】:

    • 我有一个问题,如果我们用偏移量20查询,配额请求是否算作20?还是包括前20个,总共40个?谢谢
    • 如果启用分页,有没有办法获取查询中的文档总数?因为访问快照的计数属性只返回该分页块中的文档计数。有没有办法知道有多少文档超出了分页结果中已经显示的内容?
    • 不。服务器必须为此访问每个文档,这违背了分页的目的。如果您想廉价且简单地获取文档数量,最好在数据库中保留一个计数器,并在添加/删除文档时对其进行更新。见firebase.google.com/docs/firestore/solutions/…
    • @FrankvanPuffelen 那么如果我们最初想知道所有页面,如何使用 Firestore 对数据进行分页?谢谢
    【解决方案3】:

    这可能有助于在没有页码的情况下进行分页(上一个和下一个):

    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);
              }
          })();
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 2019-01-13
      • 2019-05-29
      • 1970-01-01
      • 2018-06-11
      • 2020-08-23
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多