How to avoid FindAll condition set to "undefined" by default

I created a webapp to show the companies in my database. I have inserted two filters to improve the search: type and country.

My problem is that when the filters aren't set a query with:

type = "undefined" & country = "undefined" is sent.

Similarly, if you set only one filter, the other is undefined and vice versa. The only way to send correct queries is to set all filters to give them a value.

How can I make sure that an unset filter is not considered?

My Front-end:

const handleFetchWithSearchParameters = () => {
TutorialDataService.findByParameters(searchParameters)             
.then(response => { setTutorials(response.data); 
console.log(response.data);             
})             
.catch(e => { console.log(e);             
});    }

return (
<Form.Control as="select"type="text"
required
value={searchParameters.searchCountry}
onChange={onChangeSearchCountry}
name="country">
  <option>Select Country</option>
  <option>Nigeria</option>
  <option>Ghana</option>
  <option>Kenya</option>
  <option>Senegal</option>
</Form.Control>
<Form.Control as="select"type="text"id="type"
requiredvalue={searchParameters.searchType}
onChange={onChangeSearchType}
name="type">

  <option>Select Type</option>    
  <option>Agricultural</option>    
  <option>Manufacturing</option>    
  <option>Industrial</option>        
  <option>Livestock</option>        
  <option>Service Industry</option>
 </Form.Control>
 <button type="button" onClick={handleFetchWithSearchParameters}>    
Search                      
</button>

Service.js:

const findByParameters = searchParameters
 => {const { searchType, searchCountry} = searchParameters;
return http.get(`/tutorials?type=${searchType}&country=${searchCountry}`); }; 

Controller.js:

exports.findAll = (req, res) => { 
const { type, country } = req.query;
let condition = (type || country) ?   
{type: type,
country: country,  
} : null; 

Tutorial.findAll({
where: condition,
order: [  ['createdAt', 'DESC']] })    
.then(data => {
res.send(data);    
})    .
catch(err=> {
res.status(500).send({message:err.message || "Some error occurred while retrieving tutorials."      
});    });};

1 answer

  • answered 2020-11-20 11:55 Francesco Esposto

    In your service.js:

    const findByParameters = searchParameters  => {
      const {
        searchType,
        searchCountry
      } = searchParameters;
      const params = new URLSearchParams();
      if (searchType)
        params.set('type', searchType);
      if (searchCountry)
        params.set('country', searchCountry);
      return http.get(`/tutorials?${params.toString()}`);
    }
    

    I'm not sure which database you're using, but I think I think you can switch this:

    let condition = (type || country) ?   
    {type: type,
    country: country,  
    } : null; 
    

    With this:

    const condition = {};
    if (type)
      condition.type = type;
    if (country)
      condition.country = country;
    

    In controller.js.