Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

487
Views
How can i add where condition in firebase query based on if condition?

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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(...)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!