0

I am fairly new to Node.js and I want to execute multiple searches. I try to use req.params to get the search values from the textbox, but my code is not working. I tried to console the req.params.city , but nothing is displayed. I am using MySQL database.

Please see my code below:

Index.js

  router.get('/searche/:city/:date',function(req,res){
  let q = [req.params.city];
  console.log(q)
  db.query('SELECT  city_name, state_name, party_name, price, image, 
address, full_name  FROM register natural join party where userid = id and city_name LIKE "%'+req.params.city+'%" ' ,function(err, rows, fields) {
  if (err) throw err;
  res.render('test', {party: rows});
  });
 });

form.ejs

  <form action="/searche"  autocomplete="off">
        <input id="city" type="text" name="city" placeholder='Try "Minneapolis"'>
        <input placeholder="Choose Date" class="textbox-n" name = "date" type="text" onfocus="(this.type='date')" onblur="(this.type='text')" id="date">
        <button type="submit" class="searchButton"> <i class="fa fa search"></i></button>
   </form>

2 Answers 2

1

You're probably confusing params and body.

params is for PATH parameters used to parametrize the request path, say in your example, /searche/london/08-25-2019

Form data usually goes as body unless, ofcourse, you modify it somehow. So you can do:

router.get('/searche',function(req,res) {
  // req.body.city gets you city input
  ...
}

Note: You would have to mount body-parser or express's inbuilt like express.json()

Sign up to request clarification or add additional context in comments.

Comments

0

single quote in on wrong place, change this :

"%'+req.params.city+'%" ' 

to

`"...'%"+ req.params.city+ "%'"`

result :

db.query("SELECT  city_name, state_name, party_name, price, image, address, full_name  FROM register natural join party where userid = id and city_name LIKE '%"+ req.params.city+ "%'",()=> ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.