Home > Blockchain >  Mongodb autocomplete
Mongodb autocomplete

Time:07-23

I am building app and I need to implement autocomplete for search. now I'am trying to implement it using regexp and I am not sure whether it is best way because for any new character input in the search bar I need to do new request, is there better and faster way? I am not using mongo atlas

server.get("/search", async (req, res) => {
try {
    var regex = new RegExp(`${req.params.name}`, 'i');
   
    const users = await User.find({name:regex});
   
    res.status(400).send(users);
} catch (error) {
    res.status(500).send({message:error.message});
}

})

CodePudding user response:

In searches it is always recommended to use a Debouser, since it waits for the user to finish typing to make the request, your backend code is correct, you just need to implement the Debouser in the frontend:

Dependence debouser react: https://www.npmjs.com/package/use-debounce

you can put a timeout of 500 milliseconds, this will do the request when the user doesn't type for 500 milliseconds.

  • Related