gpt4 book ai didi

mysql - Sequelize LIKE 不适用于 WHERE 子句

转载 作者:行者123 更新时间:2023-11-29 16:24:13 28 4
gpt4 key购买 nike

我正在将 Sequelize 与 Node.js 一起使用。下面是我的代码。 Like 条件不适用于 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 }

最佳答案

在文件顶部添加 const Op = Sequelize.Op; 并将“Sequelize”导入到文件中。

import { Sequelize } from 'sequelize';

const Op = Sequelize.Op;

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

关于mysql - Sequelize LIKE 不适用于 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com