Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!