简体   繁体   English

当我在eggjs模型中的“findAll”函数中使用“Op.notLike”时,如何避免转义字符\\”?

[英]how can I avoid escape character \" when I use ‘Op.notLike ’ in function of "findAll " in eggjs model?

I have this code我有这个代码

    async getHisDataByTime(startTime, endTime, ids) {
        console.log("getHisDataByTime >>", startTime, endTime, ids)
        const Op = this.ctx.app.Sequelize.Op;
        const dataHis = await this.ctx.model.Datahis.findAll({
          where: {
            device_id: { [Op.in]: ids },
            datetime: {
              [Op.gte]: startTime,
              [Op.lte]: endTime
            },
            data:{
              [Op.notLike]: '%valid%',
            }
          },
          //group:['device_id'],
          order: [['datetime']],
          attributes: ['device_id', 'datetime', 'data']
        });
    
        return dataHis;
      }

which generates this sql command:生成此 sql 命令:

SELECT `device_id`, `datetime`, `data` FROM `data_his` AS `datahis` 
WHERE `datahis`.`device_id` IN ('2019083107100024', '2020010303100002') 
  AND (`datahis`.`datetime` >= '2019-09-23 00:00:00' 
  AND `datahis`.`datetime` <= '2019-09-24 23:00:00') 
  AND `datahis`.`data` NOT LIKE '\"%valid%\"' 
ORDER BY `datahis`.`datetime`;

I expected '%valid%' ,but actually got '\\"%valid%\\"' .我期望'%valid%' ,但实际上得到了'\\"%valid%\\"' The escaped character \\" appears automatically,and the mysql command performs as follows:转义字符\\"自动出现,mysql命令执行如下:

在此处输入图片说明

so, Op.notLike does not work at all in this case.所以, Op.notLike在这种情况下根本不起作用。

after I remove the escape character \\" and execute it in Navicat, the result is just what I want:我去掉转义符\\" ,在Navicat中执行,结果正是我想要的:在此处输入图片说明

but how can I avoid escape character \\" in "findAll" to fix this out? I've compared my code with the official example below, however, useless. https://sequelize.org/master/manual/model-querying-basics.html但是如何避免在“findAll”中使用转义字符\\"来解决这个问题?我已经将我的代码与下面的官方示例进行了比较,但是没用 。https://sequelize.org/master/manual/model-querying-基础知识.html

在此处输入图片说明

So far, it is not known what caused the escape character,but there are two solutions here.到目前为止,不知道是什么导致了转义字符,但这里有两种解决方案。

 1. public async query(sql: string, options: object): Promise using
 2. JSON_CONTAINS_PATH to check the key of data

code for solution 2:解决方案2的代码:

async getHisDataByTime(startTime, endTime, ids) {
        console.log("getHisDataByTime >>", startTime, endTime, ids)
        const { Op } = this.app.Sequelize;
        let validFn =  this.app.Sequelize.fn("JSON_CONTAINS_PATH", this.app.Sequelize.col("data"), 'one', '$.valid');
        const dataHis = await this.ctx.model.Datahis.findAll({
          where: {
            [Op.and]: [
              { device_id: { [Op.in]: ids } },
              {
                datetime: {
                  [Op.gte]: startTime,
                  [Op.lte]: endTime
                }
              },
              this.app.Sequelize.where(validFn, 0)
            ]
          },
          //group:['device_id'],
          order: [['datetime']],
          attributes: ['device_id', 'datetime', 'data']
        });
    
        return dataHis;
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能逃脱? 使用node.js时的字符表示HTTP get,以便模拟http请求? - How can I escape '?' character when using node.js express HTTP get, in order to mock http requests? 当我使用 Op.and 和 Op.ne 时,Sequelize 不起作用 - Sequelize do not work when I use Op.and and Op.ne 我如何检索 static 值作为续集 findAll 中的列 - how can i retrieve static value as column in sequelize findAll sequelize nodejs 我可以用 1 个 findAll 找到自定义数组中的所有电子邮件吗? - sequelize nodejs can i findAll the emails in custom array with 1 findAll? 我如何在 ejs 中使用脚本 function? - how can i use script function in ejs? 我可以在同一个表的同一个 api 中使用多个 findAll() 来获取数据吗? - Can I use more than one findAll() in same api on same table to get the data? 当被调用函数本身在上下文中时,如何避免nodejs vm脚本函数调用丢失上下文? - How do I avoid nodejs vm script function call losing context when called function is itself in the context? 安装我的npm模块时如何逃避预构建测试? - How can i escape pre build tests when installing my npm module? 在生产模式下将源与 webpack 捆绑在一起时,如何避免丢失类名? - How can I avoid losing my class name when I bundle source with webpack in production mode? 我如何在使用sequelize的包含模型中使用限制 - how can i use limit in include model using sequelize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM