最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knex where doesn't allow passing single string to `.where()` - Stack Overflow

programmeradmin0浏览0评论

I have the following line in a program written in Node.js using Knex and 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")
         .select()

Where db is a variable created by calling knex(config). However, the function raw(sql) doesn't seem to work, as it keeps throwing this error at runtime:

TypeError: The operator "undefined" is not permitted at Formatter.operator (I:\git\server\node_modules\knex\lib\formatter.js:138:13)

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

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

What am I doing incorrectly?

If it's relevant, I'm writing in Typescript, and as you can see with await, I am using ES6. However, this query executes fine if I exclude the with(), and with() refuses to accept something that is not created by raw().

Edit:

If I test this, it shows that the problem is in with() and not in raw():

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

Gives the expected output.

I have the following line in a program written in Node.js using Knex and 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")
         .select()

Where db is a variable created by calling knex(config). However, the function raw(sql) doesn't seem to work, as it keeps throwing this error at runtime:

TypeError: The operator "undefined" is not permitted at Formatter.operator (I:\git\server\node_modules\knex\lib\formatter.js:138:13)

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

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

What am I doing incorrectly?

If it's relevant, I'm writing in Typescript, and as you can see with await, I am using ES6. However, this query executes fine if I exclude the with(), and with() refuses to accept something that is not created by raw().

Edit:

If I test this, it shows that the problem is in with() and not in raw():

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

Gives the expected output.

Share edited Nov 7, 2017 at 21:19 Mikael Lepistö 19.8k4 gold badges72 silver badges72 bronze badges asked Nov 2, 2017 at 1:28 user7706068user7706068 3
  • What is the value of the name variable? – Matt Commented Nov 2, 2017 at 1:51
  • @Matt if it matters, it's just a string like "hello world" – user7706068 Commented Nov 2, 2017 at 1:55
  • Just checking if that was the undefined value. – Matt Commented Nov 2, 2017 at 2:04
Add a ment  | 

1 Answer 1

Reset to default 7

Turns out the problem was with where(), which I didn't see until I inspected the stack trace more closely. Replacing .where("idx > 0") with .where("idx", ">", 0) fixed it.

发布评论

评论列表(0)

  1. 暂无评论