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

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

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 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