【问题标题】:Knex where doesn't allow passing single string to `.where()`Knex where 不允许将单个字符串传递给`.where()`
【发布时间】:2017-11-02 01:28:47
【问题描述】:

我在使用 Knex 和 SQLite 用 Node.js 编写的程序中有以下行:

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) 函数似乎不起作用,因为它在运行时不断抛出此错误:

TypeError:不允许使用“未定义”运算符 在 Formatter.operator (I:\git\server\node_modules\knex\lib\formatter.js:138:13)

在 QueryCompiler_SQLite3.whereBasic (I:\git\server\node_modules\knex\lib\query\compiler.js:525:100)

在 QueryCompiler_SQLite3.where (I:\git\server\node_modules\knex\lib\query\compiler.js:314:32)

我做错了什么?

如果相关,我正在使用 Typescript 编写,正如您在 await 中看到的那样,我使用的是 ES6。但是,如果我排除了with(),并且with() 拒绝接受不是由raw() 创建的内容,则此查询可以正常执行。

编辑:

如果我对此进行测试,则表明问题出在with() 而不是raw()

console.log("name: " + name);
console.log("db.raw(name): " + db.raw(`instr(items.name, ?) asc`, name));

给出预期的输出。

【问题讨论】:

  • name 变量的值是多少?
  • @Matt 如果重要的话,它只是一个像“hello world”这样的字符串
  • 只是检查这是否是undefined 值。

标签: javascript node.js sqlite typescript knex.js


【解决方案1】:

原来问题出在where(),直到我更仔细地检查了堆栈跟踪,我才看到它。用.where("idx", ">", 0) 替换.where("idx > 0") 修复了它。

【讨论】:

    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多