【问题标题】:Cloud firestore not ordering problems when query查询时 Cloud Firestore 没有排序问题
【发布时间】:2020-09-15 15:19:17
【问题描述】:

我的查询不是订购。实际上它没有反应。尝试此操作时,我收到一个空的结果屏幕:

let stateQuery = firestore.collection('categories').where('user', '==', uid).orderBy('name'); 

        stateQuery.onSnapshot((querySnapshot) => {
          const docs = [];
          querySnapshot.forEach((doc) => { 
            docs.push({ ...doc.data(), id: doc.id });
          });
          setCategories(docs);
        });
      };

我也尝试过这样做,但没有:

 let stateQuery = firestore.collection('categories').where('user', '==', uid);
 let stateQuery2 =  stateQuery.orderBy('name');

【问题讨论】:

  • 除了您观察到的内容之外,您究竟希望这个查询做什么?请编辑问题以使其清晰,显示您希望在结果中看到的文档的具体示例。
  • 对不起,我不明白你的要求。如果我不使用 orderBy,则查询会带来所有集合,这没关系。我想要相同的结果,但按“名称”字段排序。当我使用 orderBy 结果为空。
  • 您的字段“名称”是数据库中的数字吗?
  • 您能否确认categories 文档有一个字段name,并带有一个值? (参见 doc =>“orderBy() 子句还过滤给定字段的存在。结果集将不包括不包含给定字段的文档。”)
  • 是的,有一个字段“name”

标签: javascript firebase google-cloud-firestore redux-firestore


【解决方案1】:

问题是您的查询需要复合索引。有关复合索引的文档位于 this page

检查控制台是否有错误消息,其中应包含创建复合索引所需的链接。当我在我的数据库上运行类似的查询时,我得到了这个链接

https://console.firebase.google.com/v1/r/project/(PROJECTNAME)/firestore/indexes?create_composite=Xd1.....XTY

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多