0

I have made a search bar on the customers page of my website and whatever string the admin enters in the search bar will be sent as a get request, and based on the input I am trying to find all the data in MySQL db which contains the input string inside of the fullname field.

My website build as MVC model, using Express to display data and Node.js

This is my form

<form class="d-flex"  method="GET">
     <input class="form-control me-2 py-1" type="text" id="search" name="search" placeholder="Search customer name" aria-label="Search" value="<%= %>" />
     <button class="btn btn-sm btn-secondary" type="submit">Search</button>
</form>

This is route in web.js file

router.get('/customer?search',authentication.handleAuthentication, authadmin.authAdmin,adminController.searchCustomer);

getCustomerByName() inside adminServices.js

let getCustomerByName = (name) => {

return new Promise(async (resolve, reject) => {
  try {
    let user = await db.User.find({ fullname: name });

    if (user) {
      console.log(user);
      resolve(user);
    } else {
      resolve(user);
    }
  } catch (e) {
    reject(e);
  }
});

};

searchCustomer() inside adminController.js

let searchCustomer = async (req,res,next) =>{
    let name = req.params.search;
    
    let customer = await adminServices.getCustomerByName(name);
    
    return res.render('admin/customer.ejs', {
        customer: customer,
    });
}

I had tried req.body.search / req.params.search / req.query but seem like it can't get the input. The URL like this: http://localhost:8080/customer?search=mai. I couldn't find where is the problem because there is nothing show in the console. Are there any method I could try?

dee
  • 2,244
  • 3
  • 13
  • 33
LiaoMie
  • 3
  • 4
  • Your get route is not correct. You dont need ?search. https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express – Erenn Nov 19 '21 at 10:50
  • @Erenn I changed it into router.get('/customer',authentication.handleAuthentication, authadmin.authAdmin,adminController.searchCustomer); but it still not working – LiaoMie Nov 19 '21 at 11:09
  • Does any request generated at all? Does your '/customer' route getting triggered? – Erenn Nov 19 '21 at 11:11
  • @Erenn Yes , I have another route get /customer for get a customer list. What should I do to trigger /customer but with a search value – LiaoMie Nov 19 '21 at 12:24
  • Check my answer. You need to change 3 things. – Erenn Nov 19 '21 at 12:25

1 Answers1

0

You need to add action tag to form element. Change route name to just customer and use req.query.search in controller.

router.get('/customer', authentication.handleAuthentication, authadmin.authAdmin, adminController.searchCustomer);



let searchCustomer = async(req, res, next) => {
  let name = req.query.search; // change params to query

  let customer = await adminServices.getCustomerByName(name);

  return res.render('admin/customer.ejs', {
    customer: customer,
  });
}
<form class="d-flex" action="/customer" method="GET">
  <input class="form-control me-2 py-1" type="text" id="search" name="search" placeholder="Search customer name" aria-label="Search" value="<%= %>" />
  <button class="btn btn-sm btn-secondary" type="submit">Search</button>
</form>
Erenn
  • 625
  • 6
  • 18