【发布时间】:2021-03-01 22:43:43
【问题描述】:
前端:
const [searchParameters, setSearchParameters] = useState({
type: "",
country:"",
});
const onChangeSearchType = e => {
const workingObject = {...searchParameters};
workingObject.searchType = e.target.value;
setSearchParameters(workingObject);
};
const onChangeSearchCountry = e => {
const workingObject = {...searchParameters};
workingObject.searchCountry = e.target.value;
setSearchParameters(workingObject);
};
const handleFetchWithSearchParameters = () => {
TutorialDataService.findByParameters(searchParameters)
.then(response => {
setTutorials(response.data);
console.log(response.data);
})
.catch(e => {
console.log(e);
});
}
return()之后:
<Form.Control as="select" defaultValue=""
type="text"
className="form-control"
id="country"
required
value={searchParameters.country}
onChange={onChangeSearchCountry}
name="country">
<option>Nigeria</option>
<option>Ghana</option>
<option>Kenya</option>
<option>Senegal</option>
</Form.Control>
<Form.Control as="select" defaultValue=""
type="text"
className="form-control"
id="type"
required
value={searchParameters.type}
onChange={onChangeSearchType}
name="type">
<option>Agricultural</option>
<option>Manufacturing</option>
<option>Industrial</option>
<option>Livestock</option>
<option>Service Industry</option>
</Form.Control>
<div className="input-group-append">
<button
className="btn btn-outline-secondary"
type="button"
onClick={handleFetchWithSearchParameters}
Search
</button>
Service.js:
import http from "../http-common.js";
const findByParameters = searchParameters => {
// This is the destructuring syntax I've linked above
const { type, country, creditscore, interest } = searchParameters;
// Here we use & ampersand to concatinate URL parameters
return http.get(`/tutorials?type=${type}&country=${country}&creditscore=${creditscore}&interest=${interest}`);
};
export default {
findByParameters
};
Controller.js:
// Retrieve all Industries from the database.
exports.findAll = (req, res) => {
const type = req.query.type ;
let condition = type ? { type : { [Op.like]: %${type }% } } : 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."
});
}); };
因此,我的网络应用程序的此页面用于显示保存在我的数据库中的所有公司的列表。
我创建了一个过滤器,允许您通过findByType 仅显示特定类型的过滤器。
我想插入其他过滤器,例如:findByRevenue、findByEmployeesNumber。
不知道是否应该为每种情况在前端和后端都编写新函数?还是有更聪明的方法?
此外,过滤器不必单独工作,它们还需要组合在一起以改进您的搜索。我希望我已经很好地解释了它应该如何工作,它就像任何电子商务网站一样。
编辑:我按照建议更改了代码,但我仍然遇到问题。它不再让我使用输入表单。事实上,请求是空的,例如:
type = ""
country = ""
我觉得input.value =有问题
【问题讨论】:
标签: javascript mysql node.js reactjs sequelize.js