【问题标题】:Sequelize query return where like and where id matchSequelize 查询返回 where like 和 where id 匹配
【发布时间】:2020-08-02 14:21:57
【问题描述】:

查询应该只返回与employeeId 匹配的记录,并且文件名与context.data.filename 类似,但它返回的代码存在问题,即使employeeId 不匹配。对这个家伙有任何想法吗?谢谢。

#代码

const file = await context.service.Model.findAll({
    query: {
      employeeId: record.id,
      $or: [
        {
          filename: {
            $like: context.data.filename,
          },
        },
      ],
    },
    paranoid: false,
  });

【问题讨论】:

  • 我认为您想使用 $and 来获取具有给定 ID 和文件名的员工
  • @Sheelpriy,你能发表一下你的意思吗?

标签: javascript node.js typescript sequelize.js


【解决方案1】:

您不需要添加 OR,OR 与单个参数返回 true。试试这个:

const file = await context.service.Model.findAll({
    query: {
     $and: [
        {
         employeeId: record.id,
          filename: {
            $like: context.data.filename,
          },
        },
      ],
    },
    paranoid: false,
  });

【讨论】:

  • 我试过这个先生它仍然返回匹配文件名而不是employeeId的记录。只有当employeeId 和文件名匹配时它才应该返回
  • 写 where 而不是查询,可能会有所帮助。刚刚更新了代码sn-p
  • 当我使用 where 它给出错误错误:错误:无效值 { '$like': 'DOC.doc' }
  • 那么你必须添加 $add 而不是 or。这肯定会工作
  • 对不起,我试过还是一样,我试过用 where: { employeeId: record.id, filename: { [Op.like]: %${context.data.filename}%, }, },跨度>
猜你喜欢
  • 2014-03-03
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 2018-09-25
相关资源
最近更新 更多