javascript - Sequelize 搜索 API 无法按预期运行 expressJS

标签 javascript node.js express sequelize.js

我想搜索所有列并显示搜索结果。
我是 Sequelize 的新手,因此使用此代码来执行此操作,但是这里正在检查完整匹配
我也想显示部分匹配的详细信息
我如何实现这一目标?

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

关于javascript - Sequelize 搜索 API 无法按预期运行 expressJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67054982/

相关文章:

javascript - 是否有用于 Koa.js 的 Express 式嵌套路由器?

javascript - AngularJS 等待 $resource 响应设置值

node.js - Electron 安装错误 : Failed at the electron@1. 4.6 安装后脚本 'node install.js'

javascript - nodejs jsonp - 访问控制允许来源错误

node.js - 如何使用 2048 位的 Diffie-Hellman 来创建密码?

node.js - Sequelize one to Many 插入不起作用?

javascript - 无效日期 : Converting UTC to local

javascript - Vue.js AJAX 列表渲染检查是否准备好?

javascript - 如何使用文件夹上传文件?

node.js + 表达错误 : cannot read property 'url' of undefined