I am writing a query in firebase, but I want to add where() condition based on the condition. Like if some values are exist then where() should be included otherwise normal query should be run as it is. Below is my code. I checked using console.log if condition is passed but the query returns all results. Please help me to fix this issue.
const loadPapers = () => {
let t = savedTopifyPapers
if (queryParams.value.key && queryParams.value.value) {
t.orderBy('timeStamp')
.limit(limit.value)
.where(queryParams.value.key.toString(), '==', queryParams.value.value)
} else {
t.orderBy('timeStamp').limit(limit.value)
}
t.onSnapshot((snapshot) => {
const _lastDoc = snapshot.docs[snapshot.docs.length - 1]
firstVisible.value = snapshot.docs[0]
if (_lastDoc) {
lastDoc.value = _lastDoc
}
if (snapshot.empty) {
loadingData.value = false
}
papersData.value = snapshot.docs.map((doc) => {
const data = doc.data()
const id = doc.id
loadingData.value = false
return { id, ...data }
})
papersDataLength.value = papersData.value.length
})
}
Any solution appreciated!
just take reference of collection.
let query = firebase.firestore().collection(collectionName).
query = query.where(...)
query = query.where(...)
and with use of condition
if (condition) {
query = query.where(...)
}
execute query after all clause add.
query.get().then(...)