javascript - How to reduce document reads for Firestore - Stack Overflow

I am using Firestore (I am new to this) for small web application. Currently, each time when I refresh

I am using Firestore (I am new to this) for small web application. Currently, each time when I refresh or go to another page, the function retrieves all the documents in the Firestore. But the data that it retrieves does not change often,

Is there a way for me to retrieve all the data that will cost me little to no document reads?

I am currently using these function to retrieve the data

firebase
.firestore()
.collection("products")
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});



firebase
.firestore()
.collection("products")
.where("prodID", "==", prodID)
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});

I am using Firestore (I am new to this) for small web application. Currently, each time when I refresh or go to another page, the function retrieves all the documents in the Firestore. But the data that it retrieves does not change often,

Is there a way for me to retrieve all the data that will cost me little to no document reads?

I am currently using these function to retrieve the data

firebase
.firestore()
.collection("products")
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});



firebase
.firestore()
.collection("products")
.where("prodID", "==", prodID)
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});
Share Improve this question edited Oct 19, 2021 at 17:17 Dharmaraj 51.1k8 gold badges67 silver badges98 bronze badges asked Oct 19, 2021 at 16:06 Chan Wei JieChan Wei Jie 831 silver badge6 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

It depends on your app.
But a way to reduce it would be to retrieve them from the cache.
According to the docs (https://firebase.google./docs/reference/android//google/firebase/firestore/Source) you can do something like

function getData() {
   firebase
   .firestore()
   .collection("products")
   .get({source: "cache"})
   .then((snapshot) => {
         if (!snapshot.exist) return getServerData()
         snapshot.forEach((docs) => {
       });
  });
}

function getServerData() {
   firebase
   .firestore()
   .collection("products")
   .get()
   .then((snapshot) => {
         snapshot.forEach((docs) => {
       });
  });
}   

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477066a4629393.html

相关推荐

  • javascript - How to reduce document reads for Firestore - Stack Overflow

    I am using Firestore (I am new to this) for small web application. Currently, each time when I refresh

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信