Dha*_*raj 12 javascript firebase google-cloud-firestore
如何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()
Run Code Online (Sandbox Code Playgroud)
Dha*_*raj 37
选项 1:QueryConstraint使用以前的作为基础有条件地添加
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);
Run Code Online (Sandbox Code Playgroud)
选项 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2396 次 |
| 最近记录: |