bradtraversy / proshop_mern

Shopping cart built with MERN & Redux
1.99k stars 1.18k forks source link

search bug when the search term contains () after a word #145

Closed SAYEED05 closed 3 years ago

SAYEED05 commented 3 years ago

when trying to search, if there is () after a word in the search term the result is empty.

for example if there is a product with name 'playstation (white)' ,if i search 'playstation' or 'white' or even '(white)' the result shows.but if i search 'playstation (white)' the result is empty.

i think the bug is due to regex because when i tried to type for example: 'playstation ('

this error occurs Regular expression is invalid: missing ) .but i don't know how to resolve it.

when i tried to search 'playstation /(white/),

lets say there are 3 products : playstation (white),playstation (black),xbox

it shows playstation (white) and playstation (black)

i tried it with this repo also.

basir commented 3 years ago

hello there

one simple solution can be replacing ( with /( in the search term in this line:

https://github.com/bradtraversy/proshop_mern/blob/master/backend/controllers/productController.js#L14

using this code:

$regex: req.query.keyword.replaceAll('(','/(').replaceAll(')','/)'),

sample code:

const s = "shirt (white) (green)"; console.log(s.replaceAll('(','/(').replaceAll(')','/)')); result:

"shirt /(white/) /(green/)"

SAYEED05 commented 3 years ago

not working.Like i said before the problem is not when i try to search the words with brackets, its when i try to search words inside and outside brackets. for example if there is a product named playstation (white) when i search playstation or (white) it works fine.But when i search playstation (white) it returns no products

basir commented 3 years ago

what I suggest is to get words from the search term and use logical OR to search for them. like: playstation (white) => [playstation, white] => filter by name:playstation or name:white