【发布时间】:2022-01-12 16:37:06
【问题描述】:
我已经创建了 react 应用,并且正在使用 firebase firestore。 所以在开始使用时我没有任何问题,直到我发现我需要创建这样的查询:
db.collection('schedule')
.where('working_day', '>=', new Date())
.where('master', '==', masterId)
.orderBy('working_day', 'desc')
.get();
我有一个名为 schedule 的集合,这个 schedule 集合包含 文档 列表。每个文档都有结构:
working_day: timestamp;
start_hour: number;
end_hour: number;
master: string;
当我调用此查询时 - 我没有看到任何结果(请求消失了,但根本没有结果)。有人建议我添加复合索引,它必须解决这个问题 我使用 firestore 索引选项卡做到了,现在我有了这个索引:
Collection ID Fields indexed Query scope Status
schedule working_day Descending master Descending Collection Enabled
已成功申请 - 但我的请求仍然没有任何结果。 此外,我知道 firestore 可能会注意到我需要应用此索引并提供链接 - 但我没有看到此链接。我应该在哪里找到这个?还是我做错了什么?
头疼了 2 天,没找到为什么会出现这个问题
【问题讨论】:
-
我还尝试了不同的索引变体和按任何字段排序 - 无论如何都没有结果
-
当没有正确的索引与查询匹配时,Web 开发者控制台中将出现要求您创建复合索引的消息。您可以尝试删除当前索引以检查它是否出现。我快速测试了重新创建您的文档和查询,我确实看到了消息,创建了索引(允许它构建),然后实际获取了文档。如果仍然没有输出,您是否确保此查询将匹配您数据库中的文档?
-
为什么是
master Descending而不是master Ascending?
标签: javascript reactjs firebase google-cloud-firestore