【问题标题】:How to avoid FindAll condition set to "undefined" by default如何避免 FindAll 条件默认设置为“未定义”
【发布时间】:2021-03-03 19:30:19
【问题描述】:

我创建了一个 web 应用程序来显示我数据库中的公司。我插入了两个过滤器来改进搜索:类型和国家/地区。

我的问题是,当过滤器没有设置查询时:

type = "undefined" & country = "undefined" 已发送。

同样,如果您只设置一个过滤器,则另一个未定义,反之亦然。发送正确查询的唯一方法是设置所有过滤器以赋予它们一个值。

如何确保不考虑未设置的过滤器?

我的前端:

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."      
});    });};

【问题讨论】:

    标签: mysql node.js reactjs sequelize.js


    【解决方案1】:

    在您的 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()}`);
    }
    

    我不确定您使用的是哪个数据库,但我认为您可以切换这个:

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

    有了这个:

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

    在 controller.js 中。

    【讨论】:

    • 非常感谢,我永远不会独自完成。你能告诉我在哪里学习这个主题吗?但是现在我在 controller.js 中遇到了问题,因为显然如果我只发送类型参数,我会收到错误“WHERE parameter \ "country \" has invalid \"undefined \" value"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多