I'm trying to search for words through queries but it gives me this message:
The problem occurs when writing this code
const search = String(req.query.keyword)
if (req.query.keyword) {
req.query.keyword = req.query.keyword.split(""").join(" ");
console.log(req.query.keyword)
const query = {};
query.$or = [
{title: { $regex: search, $options : 'i' }},
{description: { $regex: search, $options : 'i' }}
];
mongooseQuery = mongooseQuery.find(query)
}
CodePudding user response:
I checked the code again and the solution to the problem is to replace the if with this code
.find(req.query.keyword ? query : {})
const query = {};
query.$or = [
{ title: { $regex: req.query.keyword, $options: "i" } },
{ description: { $regex: req.query.keyword, $options: "i" } },
];