I want to return a JSON from MongoDB with some filters, the problem is, when I try to pass a variable through the function, it does not work, this is the code:
async find(req, res) {
const { filter, input } = req.body;
console.log(filter);
console.log(input);
const clients = await Client.find({ filter : { '$regex' : input, '$options' : 'i' } });
console.log(clients);
return res.json(clients);
},
The logs consoles are:
name
testinput
[]
If I change:
const clients = await Client.find({ filter : { '$regex' : input, '$options' : 'i' } });
by:
const clients = await Client.find({ 'name' : { '$regex' : input, '$options' : 'i' } });
It works, but why can not I pass a variable? I need to search with several different filters