Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

481
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda