Pagination in firestore api

后端 未结 3 1788
故里飘歌
故里飘歌 2020-12-07 02:18

I am trying to paginate documents with firestore beta. I am following the official docs

I see there is a pageSize param to say how many documents you want to show, b

相关标签:
3条回答
  • 2020-12-07 03:06

    Firestore range queries are based on an having anchor document. So you must know the (order-by properties of the) document that the range start with, and then use ref.startAfter(anchorDdoc).limit(10) (or ref.startAfter(valueOfAnchorDoc).limit(10)) to get the next page.

    The Firestore server-side Admin SDKs have an offset() call that allows the server to determine the document to starts at. But the client-side SDKs do not have this method.

    0 讨论(0)
  • 2020-12-07 03:16

    From your documentation link, I assume that you're using the REST API. In the REST API, there is a pageToken parameter, which you can specify. This can be derived from the nextPageToken returned from the previous request.

    Previous response

    {
      "documents": [
        {
          object(Document)
        }
      ],
      "nextPageToken": ABCDEF1234567890,
    }
    

    Next request

     projects/my-project/databases/my-database/documents or projects/my-project/databases/my-database/documents/chatrooms?pageSize=20&pageToken=ABCDEF1234567890
    
    0 讨论(0)
  • 2020-12-07 03:17

    This may help paginating without page numbers (previous and next):

    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);
              }
          })();
        });
    
    0 讨论(0)
提交回复
热议问题