I am encoding a query with encodeURIComponent and then using it in backend but it isn't working.
Db : mongodb Odm : mongoose
What i do is :
encodeURIComponent("{ name: { $regex: 'da', $options: 'i' }}")
which is %7B%20name%3A%20%7B%20%24regex%3A%20'da'%2C%20%24options%3A%20'i'%20%7D%7D
I send it like this:
/api/nft/search?q=%7B%20name%3A%20%7B%20%24regex%3A%20%27da%27%2C%20%24options%3A%20%27i%27%20%7D%7D
I use it like this :
exports.searchNftByName = (req, res) => {
console.log(req.query.q);
Nft.find(req.query.q).exec((err, nft) => {
if (err || !nft) {
return res.status(400).json({
status: 'fail',
message: 'NFT not found!',
});
}
return res.status(200).json({
status: 'success',
message: 'We found something',
nft,
});
});
};
console.log(req.query.q); out is { name: { $regex: 'da', $options: 'i' }}
When i copy it from console and use it directly to find the data it works.