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

161
Vistas
Using the spread operator on an array of filters to create a Firestore query

I'm working on a filter component for my React web app and want to return (pass through props) an array of conditions for the Firestore query.

For example if the user wants to filter out posts with no comments, they check a checkbox and a where("commentsCount", ">", "0") is added to the array of filters.

  const queryParams = [
    collection(db, "posts"),
    orderBy(sortType, sortMethod),
    limit(POSTS_PER_PAGE),
  ];
  const filters = [
    where("commentsCount", ">", "0"),
    // And possibly more where's
  ];
  queryParams.push(...filters);
  
  // Create a firestore query object using the parameters
  const _query = query(...queryParams);

But doing this gives me a TypeScript error: A spread argument must either have a tuple type or be passed to a rest parameter. TS2556.

What am I doing wrong?

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

0

You cannot pass an argument of type (CollectionReference<DocumentData> | QueryConstraint)[] in query. Try adding collection() directly in query and removing it from queryParams so it'll be an array of type QueryContraint[]:

const queryParams = [
  orderBy(sortType, sortMethod),
  limit(POSTS_PER_PAGE),
];

const filters = [
  where("commentsCount", ">", "0"),
  // And possibly more where's
];
  
// Create a firestore query object using the parameters
const _query = query(collection(db, "posts"), ...queryParams);
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