javascript - 使用 Modular SDK v9 的 Firestore 条件 where 子句
问题描述
where()
如何使用 Firebase Modular SDK (v9)执行带有条件子句的查询?
名称空间版本 (v8) 中的示例查询:
const status = "live"
const publishedAfter = 1630607348811
let q = firebase.firestore().collection("articles")
// filters selected by users
if (status) q = q.where("status", "==", "live")
if (publishedAfter) q = q.where("publishedAt", ">", publishedAfter)
const qSnapshot = await q.get()
解决方案
QueryConstraint
选项 1:使用以前的作为基础有条件地添加
let q = query(collection(firestore, "articles"))
// filters selected by users
if (status) q = query(q, where("status", "==", "live"))
if (publishedAfter) q = query(q, where("publishedAt", ">", publishedAfter))
const qSnapshot = await getDocs(q);
选项 2:有条件地添加QueryConstraints
到数组
const constraints = []
// filters selected by users
if (status) constraints.push(where("status", "==", "live"))
if (publishedAfter) constraints.push(where("publishedAt", ">", publishedAfter))
const q = query(collection(firestore, "articles"), ...constraints)
const qSnapshot = await getDocs(q);
推荐阅读
- angular - 在角度中使用指令 ngIf 或指令 ngFor 时显示空白页
- bash - 无法验证 shell 脚本中的字符串
- java - 是否有像“changeSelection()”这样的函数来选择行但在表格顶部而不是底部?
- java - Java Keystore - 当 varbinary 存储为 0x 时无法加载密钥库
- regex - 在不使用 Apache NIFi 中的 avroSchema 或 ConvertRecord 处理器的情况下,将 csv 文件转换为 json 的方法是什么?
- java - 我如何将这段 Java 代码转换为 kotlin?
- matlab - MATLAB - MATLAB 32bit 和 64bit 之间的交互
- android - Android Studio Gradle“排除”未按预期工作
- c - 如何在 VS Code 中控制源文件
- sql-server - DataTable 不使用 DataAdapter (VB.NET) 更新数据库