javascript - 从 Firestore 获取记录进行分页 - Ionic/Angular

标签 javascript google-cloud-firestore

我正在尝试按照文档从 Firestore 中获取前 25 条记录,如下所示。

注意:我的要求是获取我尝试使用 current25 = documentSnapshots.docs;前 25 条 记录,它不会返回 25 条记录的数组.

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

return first.get().then(function (documentSnapshots) {

// *** I am stuck here as I am not getting the array ***
var current25 = documentSnapshots.docs;
  console.log(current25);
  // 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);
});

最佳答案

您无法返回 promise ,因为您没有给予足够的时间来解决 promise 。为了解决您的问题,您必须使您的函数异步等待 promise 获取文档。

async function yourfunction(...){

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

   const documentSnapshots = await first.get();


   var current25 = documentSnapshots.docs;
   console.log(current25);
   // 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);

   return "What you want to return";
}

关于javascript - 从 Firestore 获取记录进行分页 - Ionic/Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60877220/

相关文章:

javascript - Bootstrap-select 不接受 id

javascript - 在 jQuery 中绑定(bind)事件后,如何获取对事件处理函数的引用?

java - 如何从 Firestore 中的子集合中获取所有文档

javascript - 如何使用Vue从数据库中检索数据

javascript - JavaScript 构造 `{}` 和 `call()` 是什么?

javascript - 从更新的项目更新时,在云函数中创建引用的 Firestore 文档

flutter - 在 flutter 中使用 StreamBuilder 时将数据传递到下一个屏幕

java - Firestore Android 分页中的 ArrayIndex 错误

javascript - 子收藏更新触发云功能

javascript - 加载页面时如何防止关注表单(在页面底部)?