0

基本上我试图实现分页,但是每次我更改startAtcurrentPage时,我的分页查询都会返回相同的结果,这是为什么?

  const db = firebase.firestore();
  let fetchedBlogs = []
  const startAt = currentPage * itemsPerPage - itemsPerPage
  const user = firebase.auth().currentUser;
  db.collection("blogs").where("userUid", "==", user.uid).orderBy("createdAt").startAt(startAt).limit(itemsPerPage).get().then(querySnapShot => {
    querySnapShot.forEach(doc => fetchedBlogs.push(doc.data()))
    return fetchedBlogs
  }).catch(err => console.log(err))
4

2 回答 2

1

它返回相同的文档,因为startAt它是常量。如果你想对文档进行分页,你必须喜欢这样的代码:

var first = db.collection("cities")
        .orderBy("population")
        .limit(25);

return first.get().then(function (documentSnapshots) {
  // Get the last visible document
  var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
  console.log("last", lastVisible);

  // Construct a new query starting at this document,
  // get the next 25 cities.
  var next = db.collection("cities")
          .orderBy("population")
          .startAfter(lastVisible)
          .limit(25);
});

https://firebase.google.com/docs/firestore/query-data/query-cursors?hl=es-419

于 2020-07-22T17:50:31.393 回答
0

好吧,我的错,我教 startAt 需要一个索引值,但它实际上需要一个字段值,例如

var first = db.collection("cities")
                  .orderBy("population")
                  .limit(25);

          return first.get().then(function (documentSnapshots) {
            // Get the last visible document
            var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
            console.log("last", lastVisible);

            // Construct a new query starting at this document,
            // get the next 25 cities.
            var next = db.collection("cities")
                    .orderBy("population")
                    .startAfter(lastVisible)
                    .limit(25);
          });
于 2020-07-22T17:49:45.593 回答