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

196
Views
Handling complex query parameters Express.Js

I'm making REST APIS with Express.js I have the following express route:

/api/customer

I added multiple query params to the route like this:

/api/customer?name=jake
/api/customer?country=america
/api/customer?name=jake&country=america 
/api/customer?name=jake&limit=10

In my controllers I handle all of these with If and there are so many cases I feel that this method would not scale, is there a better way of handling this ?

This is the code for my controller, I'm using Sequelize to query the database:

async function getAllCustomer(queryLimit, page) {  
  const customers = await Customer.findAll({
    limit: queryLimit ? parseInt(queryLimit) : null,
    offset: page ? parseInt(queryLimit) * parseInt(page) : null
  });
  
  return customers;
}

async function getCustomerByFirstName(name, queryLimit, page) {
  return await Customer.findAll({
    where: {
      firstName: name,
    }
  })
}

async function getCustomerByAddress(address) {
  return await Customer.findAll({
    where: {
      customerAddress: address
    }
  })
}

async function getCustomerByNameAddress(name, address) {
  return await Customer.findAll({
    where: {
      [Op.and]: [
        {firstName: name},
        {customerAddress: address}
      ]
    }
  })
}

async function getCustomer(req, res) {
  const page = req.query.page;
  const queryLimit = req.query.limit;
  const name = req.query.name;
  const address = req.query.address;
  
  let customers;
  /* User want to find first names */
  if (name && !address) {
    const names = name.split(",")

    customers = await getCustomerByFirstName(names, queryLimit, page)


    res.status(200).send(customers)
    return;
  }

  /* User want to find addresses */
  if (!name && address) {
    const addresses = address.split(",")
    customers = await getCustomerByAddress(addresses, queryLimit, page)
    res.status(200).send(customers)
    return;
  }

  /* User want to mix both */
  if (name && address) {
    const names = name.split(",")
    const addresses = address.split(",")
    customers = await getCustomerByNameAddress(names, addresses, queryLimit, page)    

    res.status(200).send(customers)
    return;
  }

  if (!name && !address) {
    customers = await getAllCustomer(queryLimit, page)
    res.status(200).send(customers)
    return;
  }
}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You could do something like this:

async function getCustomer(req, res) {
  const page = req.query.page;
  const queryLimit = req.query.limit;
  const name = req.query.name;
  const address = req.query.address;
  
  let query = { };
  if(name) {
    query.firstName = name;
  }
  
  if(address) {
    query.address = address;
  }
  
  let customers = await getCustomers(query, queryLimit, page);
  res.status(200).send(customers)
  return;
}

async function getCustomers(query, queryLimit, page) {  
  const customers = await Customer.findAll({
    where: query,
    limit: queryLimit ? parseInt(queryLimit) : null,
    offset: page ? parseInt(queryLimit) * parseInt(page) : null
  });
  
  return customers;
}

BTW, in your code, the functions getCustomerByFirstName, getCustomerByAddress and getCustomerByNameAddress are expecting to receive name and address as string parameter, but you are passing names and addresses array. This might lead to errors...

about 4 years ago · Santiago Gelvez 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!