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

226
Vistas
receive multiple query parameters in method GET API REST NODE

I am trying to receive multiple parameters without knowing which one arrives and which one does not, to filter a GET in the find () method

NODE JAVASCRIPT MONGOOSE

app.get('/venta',  async (req, res) =>{
    let a = req.query.anio;
    let f = req.query.fecha;
    let c = req.query.cajas;
    let q = {};
    if(a != undefined) q.anio = a;
    if(f != undefined) q.fecha = f;
    if(c != undefined) q.cajas = c;
    
    Venta.find({$or: [q]}).exec((err,ventas)=>{
        if(err) return res.status(400).json({
            ok: false,
            mensaje: 'no se encontro las ventas'
        })
        return res.json({
            ok: true,
            ventas
        })
    });
    
})

It occurred to me to put together a "q" list and insert the filters that are reached in the request. $ OR does not work

QUESTION> how can I fill the FIND () method based on the amount of filters that I am receiving in the request

thx

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

0

if you want any document that fulfill one of the conditions it needs to be a list of conditions. [q] is just an array with one element that contains all the filters with an and conjunction. to have the $or functionality it should be $or = [{anio:a},{fecha:f},{cajas:c}] for an arbitary number of filters you can loop on the key value pairs of req.query and insert them in an array accordingly. like in the example shown

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