Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

482
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda