选项 1:有条件添加QueryConstraint https://firebase.google.com/docs/reference/js/firestore_.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);
选项 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);