Using NodeJS with Mongoose and MongoDB for my back-end.
I am trying to do this (example):
controller.getUsersSearchAll = function(req, res){
let last = req.query.last;
let min = req.query.minPrice;
let max = req.query.maxPrice;
if(last && min && max){
User.find()
.where('last_name').equals(last)
.where('age').gte(min).lte(max)
.exec(function(err, result){
res.json(result);
});
}else if(last){
User.find()
.where('last_name').equals(last)
.exec(function(err, result){
res.json(result);
});
}
};
It works but in this way, it is static. The problem is when I have 10 conditions or more I had to apply many if conditions.
How to convert it to a dynamic way? Is there a smart way?