Firestore Conditional Where Clause Using Modular Sdk V9
How to perform a query with conditional where() clause using Firebase Modular SDK (v9)? Example query in name-spaced version (v8): const status = 'live' const publishedAfter = 1630
Solution 1:
Option 1: Conditionally adding QueryConstraint
using previous as base
let q = query(collection(firestore, "articles"))
// filters selected by usersif (status) q = query(q, where("status", "==", "live"))
if (publishedAfter) q = query(q, where("publishedAt", ">", publishedAfter))
const qSnapshot = awaitgetDocs(q);
Option 2: Conditionally adding QueryConstraints
to an array
const constraints = []
// filters selected by usersif (status) constraints.push(where("status", "==", "live"))
if (publishedAfter) constraints.push(where("publishedAt", ">", publishedAfter))
const q = query(collection(firestore, "articles"), ...constraints)
const qSnapshot = awaitgetDocs(q);
Post a Comment for "Firestore Conditional Where Clause Using Modular Sdk V9"