Sequelize LIKE不适用于WHERE子句

时间:2019-01-24 12:18:17

标签: mysql node.js sequelize.js sql-like

我正在对Node使用 Sequelize 。下面我我的代码。类似条件不适用于where条件。我正在尝试按公司名称添加搜索。

   let err, company;
   console.log(req.body);
   [err, company] = await to(Company.findAll({where : {company_name:{ $like: '%'+req.body.q+'%' }}}));  
   if(err){ return ReE(res, err); } 
   console.log(' company -------------- ',err);

我遇到了错误。

{ q: 'abc' }
 company --------------  { filename:
'/MYPATH/node_modules/sequelize/lib/sql-string.js',
  line: 51,
  row: 11,
  message: 'Invalid value { company_name: { \'$like\': \'%abc%\' } }',
  type: 'Error',
  stack:
   'Error: Invalid value { company_name: { \'$like\': \'%abc%\' } }\n    
   at Object.escape (/MYPATH/node_modules/sequelize/lib/sql-string.js:51:11)\n   
   at Object.escape (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:927:22)\n   
   at Object._whereParseSingleValueObject (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2396:41)\n   
   at Object.whereItemQuery (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2096:21)\n    
   at Utils.getComplexKeys.forEach.prop (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1957:25)\n    
   at Array.forEach (<anonymous>)\n    
   at Object.whereItemsQuery (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1955:35)\n    
   at Object.getWhereConditions (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2423:19)\n    
   at Object.selectQuery (/MYPATH/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1133:28)\n   
   at QueryInterface.select (/MYPATH/node_modules/sequelize/lib/query-interface.js:1077:27)\n    
   at Promise.try.then.then.then (/MYPATH/node_modules/sequelize/lib/model.js:1596:34)\n    
   at tryCatcher (/MYPATH/node_modules/bluebird/js/release/util.js:16:23)\n   
   at Promise._settlePromiseFromHandler (/MYPATH/node_modules/bluebird/js/release/promise.js:512:31)\n    
   at Promise._settlePromise (/MYPATH/node_modules/bluebird/js/release/promise.js:569:18)\n    
   at Promise._settlePromise0 (/MYPATH/node_modules/bluebird/js/release/promise.js:614:10)\n    
   at Promise._settlePromises (/MYPATH/node_modules/bluebird/js/release/promise.js:693:18)\n    
   at Async._drainQueue (/MYPATH/node_modules/bluebird/js/release/async.js:133:16)\n    
   at Async._drainQueues (/MYPATH/node_modules/bluebird/js/release/async.js:143:10)\n    
   at Immediate.Async.drainQueues [as _onImmediate] (/MYPATH/node_modules/bluebird/js/release/async.js:17:14)\n    
   at runCallback (timers.js:705:18)\n    at tryOnImmediate (timers.js:676:5)\n    at processImmediate (timers.js:658:5)',
  arguments: undefined }

1 个答案:

答案 0 :(得分:0)

在文件顶部添加const Op = Sequelize.Op;,然后在文件中添加import 'Sequelize'

import { Sequelize } from 'sequelize';

const companies = await to(Company.findAll({where : {company_name:{ [Op.like]: '%'+req.body.q+'%' }}}));
return companies;