'Getting undefined on inputs
I have tours and i need to build little filter. but on request.input am getting undefined and in final result i get all tours not filtered ones...
public async tourSearch({ request, response }) {
const tourName = request.input('tourName')
const tourLocation = request.input('tourLocation')
const tourCategory = request.input('tourCategory')
console.log(tourName)
const query = Tour.query()
if (tourName) {
query.where('title', tourName)
}
if (tourLocation) {
query.where('location', tourLocation)
}
if (tourCategory) {
query.where('category', tourCategory)
}
if (tourName || tourLocation) {
query.where('title', tourName).where('location', tourLocation)
}
if (tourName || tourCategory) {
query.where('title', tourName).where('category', tourCategory)
}
if (tourLocation || tourCategory) {
query
.where('location', tourLocation)
.where('category', tourCategory)
}
if (tourLocation || tourCategory || tourName) {
query
.where('location', tourLocation)
.where('category', tourCategory)
.where('title', tourName)
}
const filteredTours = await query.orderBy('createdAt', 'asc')
return response.json(filteredTours)
}
am new in node.js so i know that am doing something wrong but can't understand what am doing wrong can someone please help me with this...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
