Javascript Knex where不';不允许将单个字符串传递到“.where()`
在Node.js中使用Knex和SQLite编写的程序中,我有以下几行代码:Javascript Knex where不';不允许将单个字符串传递到“.where()`,javascript,node.js,sqlite,typescript,knex.js,Javascript,Node.js,Sqlite,Typescript,Knex.js,在Node.js中使用Knex和SQLite编写的程序中,我有以下几行代码: await db.table("books") .innerJoin("items", "items.id", "books.item_id") .with("idx", db.raw(`instr(items.name, ?) asc`, name)) .where("idx > 0") .orderBy("idx") .
await db.table("books")
.innerJoin("items", "items.id", "books.item_id")
.with("idx", db.raw(`instr(items.name, ?) asc`, name))
.where("idx > 0")
.orderBy("idx")
.select()
其中,db
是通过调用knex(config)
创建的变量。但是,函数raw(sql)
似乎不起作用,因为它在运行时不断抛出此错误:
类型错误:不允许运算符“未定义”
位于Formatter.operator(I:\git\server\node\u modules\knex\lib\Formatter.js:138:13)
位于QueryCompiler\u SQLite3.whereBasic(I:\git\server\node\u modules\knex\lib\query\compiler.js:525:100)
在QueryCompiler\u SQLite3.where(I:\git\server\node\u modules\knex\lib\query\compiler.js:314:32)
我做错了什么
如果相关的话,我用的是Typescript,正如您在wait
中看到的,我使用的是ES6。但是,如果我排除with()
,并且with()
拒绝接受不是由raw()
创建的内容,则此查询可以正常执行
编辑:
如果我对此进行测试,它表明问题出在with()
中,而不是raw()
:
给出了预期的输出。结果表明问题出在
where()
上,直到我更仔细地检查堆栈跟踪时才发现。将.where(“idx>0”)
替换为.where(“idx”),“>”,0)
修复了它。变量名称的值是多少?@Matt如果有关系,它只是一个类似“hello world”的字符串,只是检查这是否是未定义的值。
console.log("name: " + name);
console.log("db.raw(name): " + db.raw(`instr(items.name, ?) asc`, name));