简体   繁体   English

我刚开始使用 firebase 并在获取数据时遇到 orderBy desc function 问题

[英]I just started with firebase and have problem with orderBy desc function when getting data

In the function orderBy when passing the "desc" parameter, it does not return data, but when passing "asc" or blank, there is data.在function orderBy中传“desc”参数时不返回数据,但传“asc”或空白时有数据。

const getListProduct = async () => {
    const products = await fs
      .collection("products")
      .orderBy("createAt", "desc") //problem is here.It just works orderBy("createAt", "asc") or orderBy("createAt")
      .startAfter(0)
      .limit(5)
      .get();
    let listProduct = [];

    for (let snap of products.docs) {
      let data = snap.data();
      data.ID = snap.id;
      listProduct.push({ id: snap.id, ...data });
      if (products.docs.length === listProduct.length) {
        
        setListData(listProduct);
      }
    }
  };
     

The value in startAfter() must be either a DocumentSnapshot or the value of field to start this query after. startAfter()中的值必须是DocumentSnapshot或要在之后开始此查询的字段的值。 If createAt field is a Timestamp then it must be startAfter(<DATE_OBJ>) or snapshot of document from last query but you are passing a number.如果createAt字段是时间戳,那么它必须是startAfter(<DATE_OBJ>)或上次查询的文档快照,但您传递的是一个数字。 Try refactoring the code as shown below:尝试重构代码,如下所示:

const products = await fs
  .collection("products")
  .orderBy("createAt", "desc")
  .startAfter(new Date(2022, 8, 17, 6, 54, 0)) // for testing Date(year, date, month, hh, mm, ss)
  .limit(5)
  .get();

This should return all products where startAfter is less than the provided timestamp in descending order.这应该按降序返回startAfter小于提供的时间戳的所有产品。


Alternatively, you can store snapshot of the last query in your state and use it as shown below: 或者,您可以将最后一个查询的快照存储在 state 中并使用它,如下所示:
 let lastSnapshot = null; const q = fs.collection("products").orderBy("createAt", "desc") if (lastSnapshot) { // Query has been executed before, use last snapshot q.startAfter(lastSnapshot); } const products = await q.limit(5).get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase function 尝试写入 firebase 存储“Firebase 存储:用户无权访问”时出错 - Firebase function getting error when trying to write to firebase storage "Firebase Storage: User does not have permission to access" Firebase Android中的数据描述排序 - Firebase Data Desc Sorting in Android 我在 flutter 中遇到 firebase 身份验证问题 - i have a problem with firebase auth in flutter 当我尝试使用 termux 在 Android 上安装 firebase 时,我该如何解决这个问题? - How can I fix this problem I'm getting when I try to install firebase on Android using termux? 当我有多个唯一标识时从 Firebase 数据库读取数据 - Read data from Firebase database when I have multiple unique ids 我在 firebase 中的“onValue”语法和“set”语法之间存在连接问题 - I have a problem with connecting between 'onValue' syntax and 'set' syntax in firebase Firebase 功能:在 function 中获取文档数据 - Firebase functions: getting document data inside a function 为什么我在连接 Firebase 时遇到 Arduino 中的这个问题? - Why am I facing this Problem in Arduino when connecting Firebase? 为什么在将 env 变量加载到 Firebase function 时我得到未定义? - Why am I getting undefined when loading env variables into Firebase function? 在 firebase 中更新密码时出现“TypeError: user.getIdToken is not a function” - Getting "TypeError: user.getIdToken is not a function" when updating password in firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM