【问题标题】:Call a collection once, and filter it several times firebase Reactjs一次调用一个集合,并对其进行多次过滤 firebase Reactjs
【发布时间】:2021-03-06 23:55:35
【问题描述】:

目前我使用过滤器调用我的集合,但我想通过使用不同的 .where() 过滤同一个表中的数据来在同一页面上的多个不同表中显示我的数据。

目前,我只知道如何按调用过滤,即我将调用它并过滤它。

但我想知道是否有一种方法可以调用一次并通过过滤来调度它,而不是多次调用并过滤多次。

 function getOrders(uid) {
    fire.firestore().collection("orders")
    .where("statut", "==", "Validé")
    .orderBy('time', 'desc')
    
    .onSnapshot(function (querySnapshot) {
      setOrders(
        querySnapshot.docs.map((doc) => ({
          id: doc.id,
          orderid: doc.data().id,
          statut: doc.data().statut,
          nomproduit: doc.data().nomproduit,
          type: doc.data().type
          
        }),setLength(querySnapshot.docs.length))
        
      );
      
    });
    
  }

感谢您的宝贵时间。

【问题讨论】:

  • 从 Firestore 返回数据数组后,有什么理由不能使用 Array.filter()Array.reduce() 返回数据的新过滤版本?
  • 不知道怎么用
  • 基本上没有足够的信息来说明您想要做什么。昨晚我在评论时没有想到的一件事是您无法在 Firestore 中运行 OR 查询。如果您的数据对statut 有 20 个可能的不同值,并且您希望其中三个用于不同的视图,那么唯一的方法是三个查询;每组数据一个查询。

标签: reactjs firebase google-cloud-firestore


【解决方案1】:

您是否尝试过以下方法?我相信这是您想要的过滤类型。您可以阅读有关这些查询的更多信息 here 并搜索有用的 GCP code snippets for Firestore

// Create a reference to the cities collection
const citiesRef = db.collection('cities');

const stateQueryRes = await citiesRef.where('state', '==', 'CA').get();
const populationQueryRes = await citiesRef.where('population', '<', 1000000).get();
const nameQueryRes = await citiesRef.where('name', '>=', 'San Francisco').get();

【讨论】:

    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多