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

106
Vistas
Firestore query with empty string , 'any' or 'all'

I have filter on my website, I need to retrieve data based on the inputs in the filter. Some fields of the filter can be empty (''), I want to be able to ignore those queries related to those fields basically. Something like this

//fields
const { country, age, gender, contract, budget_low, budget_high } = queries;

//query
db.collection("users")
        .where("country", "in", country === "" ? "any" : "Nigeria")
        .where("gender", "in", gender === "" ? "any" : "Male")
        .where("age", "in", age === "" ? "any" : "21")
        .get()
        .then((querySnapshot) => {
          var data = [];
          querySnapshot.forEach((doc) => {
            data.push(doc.data());
          });
          setPeers(data);
        })
        .catch((error) => {
          console.log("Error getting documents: ", error);
        });

Is there a way to use 'any' or 'all' if the fields is empty (''), so that way I retrieve all data conditionally (if filter field exists)?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You'll want to dynamically create your query, and only add the conditions for the values that the user actually selected.

let query = db.collection("users");

if (country !== "") query = query.where("country", "==", country);
if (gender !== "") query = query.where("gender", "==", gender);
if (age !== "") query = query.where("age", "==", age);

query.get()...

In the new modular/v9 syntax, that'd be:

let conditions = []

if (country !== "") conditions.push(where("country", "==", country));
if (gender !== "") conditions.push(where("gender", "==", gender));
if (age !== "") conditions.push(where("age", "==", age));

let query = query(collection(db, "users"), ...conditions);

getDocuments(query)...
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