1
I have a method index
that returns places (houses/apartments) registered on the site. It is possible to filter by location and key characteristics if the person wants. If you do not want, I return everything that exists registered.
However, even if I do not submit any data in the request, the if
that I’m using to verify this gives true
. And no registered place is being returned.
Code:
async index(req,res){
const {location} = req.query
const {characteristics} = req.query
let spots
if({location}){
spots = await Spot.find({location: { "$regex" : location , "$options" : "i"}})//case insensitive
console.log(' local foi informado',{location})
if({characteristics}){
console.log('caracteristicas foram informadas',{characteristics})
spots = await Spot.find({characteristics: characteristics,location: { "$regex" : location , "$options" : "i"}})//case insensitive
return res.json(spots)
}
console.log('não tem caracteristicas informadas, só local')
return res.json(spots)
}
spots = await Spot.find()
return res.json(spots)
}