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>
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()
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+ "%'",()=> ...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.