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

I have the following line in a program written in Node.js using Knex and SQLite:await db.table("bo

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074309a4609743.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信