【发布时间】:2020-08-24 00:48:27
【问题描述】:
我正在尝试学习如何使用 where 条件查询 firestore。
如果我注释掉下面显示的 where 条件,则集合的内容会加载并正确排序。
我在该集合中有一个文档,其属性名为:“sharing”,其值为“open”。当我取消注释 where 条件时,我希望它是集合中唯一呈现的文档。
相反,我没有返回任何值。如果我删除 where 条件 - 所有值都会返回并按标题排序。
function useImpactMetrics() {
const [impactMetrics, setImpactMetrics] = useState([])
useEffect(() => {
firebase
.firestore()
.collection("impact_metrics")
//.where("sharing", "==", "open")
.orderBy("title")
.onSnapshot(snapshot => {
const impactMetrics = snapshot.docs.map(doc => ({
id: doc.id,
...doc.data(),
}))
setImpactMetrics(impactMetrics)
})
}, [])
return impactMetrics
}
我看过this post 和评论中引用的博客。
我已尝试启动服务器并加载内容。
在 react 中使用 where 条件和 firestore 是否需要额外的步骤?
【问题讨论】:
-
取消注释时是否在控制台中出现错误?
-
感谢@wobsoriano - 我认为只有在查询中有 2 个 where 条件时才需要索引。 orderBy 必须算作其中之一。谢谢。
标签: reactjs firebase google-cloud-firestore