Home > Back-end >  If req.query.key is empty how to ignore it on db.find() method and return all documents
If req.query.key is empty how to ignore it on db.find() method and return all documents

Time:08-09

i have two req.query arguments coming from my pug template: req.query.city and req.query.key

    form.paieskaPagalRaktiniZodi(action='/search?' method='GET')
      label(for='paieskaPagalRaktiniZodi') Key Word:
      br
      input#key(type='text' name='key')
      br
      label(for='city') City:
      br
      input#city(type='text' name='city')
      br
      button(type='submit' value='Submit' )

I want to run find method on my DB and look for documents which contains given values, but the problem is, if I don't insert anything in my pug inputs I get empty string. And if I run find method with empty string I get 0 results. Is it possible to somehow ignore find setting if given query is empty?

Now I check incoming query with if's:

exports.filtruotiSkelbimai = catchAsync(async (req, res, next) => {
  const keyName = req.query.key;
  const cityName = req.query.city;

  console.log(keyName);
  let skelbimai;
  if (keyName.length === 0) {
    const skelbimaiSelected = await Skelbimai.find({
      miestas: req.query.city,
    });
    skelbimai = skelbimaiSelected;
  }
  if (cityName.length === 0) {
    const skelbimaiSelected = await Skelbimai.find({
      $text: { $search: req.query.key },
    });
    skelbimai = skelbimaiSelected;
  }
  if (cityName.length > 0 && keyName.length > 0) {
    const skelbimaiSelected = await Skelbimai.find({
      $text: { $search: req.query.key },
      miestas: req.query.city,
    });
    skelbimai = skelbimaiSelected;
  }
  if (keyName.length === 0 && cityName.length === 0) {
    skelbimai = await Skelbimai.find();
  }
  res.status(201).render("overview", {
    title: "Skelbimai",
    skelbimai,
  });
});

Is it possible to do something like this

exports.filtruotiSkelbimai = catchAsync(async (req, res, next) => {
  const keyName = req.query.key;
  const cityName = req.query.city;

  const skelbimai = await Skelbimai.find({
    miestas: req.query.city,
    $text: { $search: req.query.key },
  });
  res.status(201).render("overview", {
    title: "Skelbimai",
    skelbimai,
  });
});

But if any given query is empty ignore that query? if both are empty, ignore them both and simply run db.find() and return all documents.

Tried to do this with $or operator, but if I indicate both req.query.key and req.query.city it returns documents only by key but ignores city.

CodePudding user response:

You can apply filters, in a cleaner way, using the spread operator:

exports.filtruotiSkelbimai = catchAsync(async (req, res, next) => {
  const keyName = req.query.key;
  const cityName = req.query.city;
  let filters = {};
  if (cityName.length > 0) {
    filters = {
      ...filters,
      miestas: cityName,
    };
  }
  if (keyName.length > 0) {
    filters = {
      ...filters,
      $text: { $search: keyName },
    };
  }
  const skelbimai = await Skelbimai.find(filters);
  res.status(201).render("overview", {
    title: "Skelbimai",
    skelbimai,
  });
});
  • Related