javascript - Sequelize 搜索 API 无法按预期工作 expressJS
问题描述
我想搜索所有列并显示搜索结果。
我是续集的新手,所以使用此代码来执行此操作,但是这里它正在检查完整匹配
我还想显示部分匹配的详细信息
我如何实现这一点?
router.post("/search-employees", async (req, res) => {
const searchTerm = req.body.searchTerm;
try {
const resp = await employee.findAll({
where: {
[Op.or]: [
{ name: searchTerm },
{ age: searchTerm },
{ country: searchTerm },
{ position: searchTerm },
{ wage: searchTerm },
],
},
});
res.status(200).send(resp);
} catch (e) {
res.status(400).send(e);
}
});
解决方案
您可以使用类似操作,例如 ([OP.LIKE], [OP.ILIKE])。在下面找到更新的查询。
router.post("/search-employees", async (req, res) => {
const searchTerm = req.body.searchTerm;
try {
const resp = await employee.findAll({
where: {
[Op.or]: [
{ name: { [Op.like]: '%' + searchTerm + '%'} },
{ age: { [Op.like]: '%' + searchTerm + '%'} },
{ country: { [Op.like]: '%' + searchTerm + '%'} },
{ position: { [Op.like]: '%' + searchTerm + '%'} },
{ wage: { [Op.like]: '%' + searchTerm + '%'} }
],
},
});
res.status(200).send(resp);
} catch (e) {
res.status(400).send(e);
}
});
推荐阅读
- node.js - MongoStore:无法初始化客户端。不接受猫鼬连接对象
- pandas - pandas 数据框中 groupby.agg 的 N 最大 / idxmax
- google-analytics - 谷歌分析跨域跟踪不起作用 - 无法通过 cid
- scala - 具有动态数据类型的 UDF
- presto - Presto 中的“查询数据所在的位置”是什么意思?
- azure-analysis-services - Azure SSAS 向上和向下缩放
- reactjs - 如何将历史参数传递给 redux-toolkit 中的路由器中间件?
- excel - 代码行 Range.PivotCell 在 Excel 2013 但不是 Excel 2017 中导致错误 (1004)
- python - 我在 python 中使用套接字模块时收到此错误:'socket.gaierror: [Errno 11001] getaddrinfo failed'
- c++ - C++ 应用程序分配的 MATLAB 访问内存