我正在尝试按照文档从 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/