例如,我的图书列表有动态过滤器,我可以在其中设置特定的颜色、作者和类别。 该过滤器可以一次设置多种颜色和多个类别。
Book > Red, Blue > Adventure, Detective.
如何有条件地添加“where”?
firebase
.firestore()
.collection("book")
.where("category", "==", )
.where("color", "==", )
.where("author", "==", )
.orderBy("date")
.get()
.then(querySnapshot => {...
最佳答案
正如您在 API 文档中看到的,collection()方法返回 CollectionReference 。集合引用扩展 Query ,并且 Query 对象是不可变的。 Query.where()和 Query.orderBy()返回新的 Query 对象,该对象在原始 Query(保持未修改)之上添加操作。您必须编写代码来记住这些新的查询对象,以便您可以继续使用它们进行链接调用。因此,您可以像这样重写代码:
var query = firebase.firestore().collection("book")
query = query.where(...)
query = query.where(...)
query = query.where(...)
query = query.orderBy(...)
query.get().then(...)
现在您可以输入条件来确定要在每个阶段应用哪些过滤器。只需为每个新添加的过滤器重新分配查询
即可。
if (some_condition) {
query = query.where(...)
}
关于javascript - 火存储 : Multiple conditional where clauses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562733/