查询中的Firebase StartAfter无法按预期工作 [英] Firebase StartAfter in query not working as expected

查看:99
本文介绍了查询中的Firebase StartAfter无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Firebase Firestore用作数据库,并且编写了Firebase函数以从Firestore数据库检索数据.

I am using firebase firestore as my database and i have written firebase functions to retrieve data from the firestore database.

我想要实现的是分页,并且根据我已经为我的firebase函数实现代码的文档.下面是代码:

What i am trying to achieve is pagination and as per the docs i have implemented the code for my firebase function. Below is the code:

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();
        });
    }
    });

});

我已经在我的Firebase函数中添加了条件,该函数可以检查是否提供了最后一个账单号.

I have added condition to my firebase function where it checks whether a last bill number is provided.

如果是,则检索最后一个帐单之后的所有帐单记录,直到设置的限制;否则,如果未提供最后一个帐单号,则将其视为第一个请求,并检索达到该限制的初始记录.

If yes then retrieve all bill records after the last bill till the set limit or else if no last bill number is provided then consider it as first request and retrieve the initial records upto the limit

无论如何,我面临的问题是,无论执行else部分的代码如何,当提供了最后一个帐单号时,查询总是从结果的开始一直返回记录,直到指定的限制为止.由于某种原因StartAfter无法正常工作

However the problem I am facing is that irrespective of code executing the else part, when the last bill number is provided , the query always return the records from the start of the result upto the limit specified. For some reason StartAfter is not working

例如,我的账单编号形式为1到25,我在上面的代码中按降序排列了它们,因此结果是从25到1的账单.

e.g i have records with bill number form 1 to 25 , i have arranged them in descending order in above code ,so the result is from bill number 25 to 1.

当没有提供帐单号时,我得到的帐单号结果为25到11. 当我提供帐单号时,我得到的是帐单号25到11的结果,而不是预期的帐单号从10到1的结果.

When no bill number is provided i get result of bill numbers from 25 to 11. When I provide the bill number i get result from bill numbers 25 to 11 instead of expected bill numbers from 10 to 1.

有人可以帮我吗?

推荐答案

我可以通过以下代码进行分页.在我的模型对象中,我使用了Document ID,据此可以找到documentReference-> documentSnapshot.我最终使用documentSnapshot进行比较(这是我的错误,因为之前我没有使用documentSnapshot)以获取所需的输出.

I could manage to paginate by below code. In my model object I have used the Document ID , based on which i find the documentReference -> documentSnapshot. I finally use the documentSnapshot to compare(this was my mistake as earlier i was not using documentSnapshot) to get the desired output.

下面是我的代码:

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");
});
});

这篇关于查询中的Firebase StartAfter无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆