gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:19:46 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com