Che*_*tan 2 javascript pagination firebase google-cloud-functions google-cloud-firestore
我使用 firebase firestore 作为我的数据库,并且我编写了 firebase 函数来从 firestore 数据库中检索数据。
我想要实现的是分页,并且根据文档,我已经为我的 firebase 函数实现了代码。下面是代码:
exports.getBillList = functions.https.onRequest((req, res) => {
let docs=[];
let limit = 15;
return cors(req, res, () => {
let lastBillInList=req.query.lastBillInList;
console.log("lastBillInList value: " + lastBillInList);
if (lastBillInList === null || lastBillInList === undefined) lastBillInList = 0;
//var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length - 1];
if(lastBillInList==0){
console.log('first time call: as no lastbillseqq');
db.collection("bills").orderBy('billNo','desc').limit(limit).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
docs.push(doc.data());
});
res.status(200).send(docs);
}).catch(function (error) {
console.error("Error getting list: ", error);
res.status(500).send();
});
}else{
console.log('second time call: as no lastbillseqq'+ lastBillInList);
db.collection("bills").orderBy('billNo', 'desc').startAfter(lastBillInList).limit(limit).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
docs.push(doc.data());
});
res.status(200).send(docs);
}).catch(function (error) {
console.error("Error getting list: ", error);
res.status(500).send();
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
我在我的 firebase 函数中添加了条件,它检查是否提供了最后一个账单号码。
如果是,则检索最后一张账单之后的所有账单记录,直到设定的限额,否则如果没有提供最后一张账单号码,则将其视为第一个请求并检索达到限额的初始记录
然而,我面临的问题是,无论执行 else 部分的代码如何,当提供最后一个账单编号时,查询总是返回从结果开始到指定限制的记录。由于某种原因 StartAfter 不起作用
例如,我有帐单编号从 1 到 25 的记录,我在上面的代码中按降序排列它们,因此结果是帐单编号从 25 到 1。
当没有提供账单号码时,我会得到从 25 到 11 的账单号码的结果。当我提供账单号码时,我会得到从 25 到 11 的账单号码的结果,而不是从 10 到 1 的预期账单号码。
任何人都可以帮我解决这个问题吗?
好吧,我发现了一个有趣的点。你必须start(afterDocument:)先打电话limit(to:),否则它不会按预期工作。所以它应该类似于以下内容:
db.collection("collection")
.order(by: "createdAt", descending: true)
.start(afterDocument: snapshot) // This is where you need to put start
.limit(to: Constants.Database.Feed.limit)
.getDocuments { (snapshot, error) in
// some useful stuff
}
Run Code Online (Sandbox Code Playgroud)
我可以设法通过下面的代码进行分页。在我的模型对象中,我使用了 Document ID ,根据它我找到了 documentReference -> documentSnapshot。我最终使用 documentSnapshot 进行比较(这是我的错误,因为之前我没有使用 documentSnapshot)以获得所需的输出。
下面是我的代码:
exports.getBillList = functions.https.onRequest((req, res) => {
console.log("----------------------------------function start");
let docs = [];
let lastBillInList=req.query.lastBillInList;
if(lastBillInList===undefined){
lastBillInList="noDocument";
}
let limit = 15;
return cors(req, res, () => {
var lastVisible = db.collection("bills").doc(lastBillInList);
lastVisible.get().then(function (doc) {
if (doc.exists) {
db.collection("bills")
.orderBy("billNo", "desc")
.startAfter(doc)
.limit(limit).get().then(function(querySnapshot){
querySnapshot.forEach(function (doc) {
//console.log(doc.data().billNo + " pushed.." + ": last bill was: " + tempBill);
docs.push(doc.data());
});
return res.status(200).send(docs);
});
} else {
db.collection("bills")
.orderBy("billNo", "desc")
.limit(limit).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
docs.push(doc.data());
});
return res.status(200).send(docs);
});
}
}).catch(function (error) {
console.log("Error getting document:", error);
return res.status(500).send();
});
console.log("----------------------------------function end");
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6457 次 |
| 最近记录: |