如何在 knex 中添加两个绑定参数?

Pav*_*dan 4 javascript mysql node.js knex.js

我正在尝试从数据库中选择一些内容,并且必须使用 2 个绑定参数。使用一个参数它可以工作,但是使用两个参数我会在nodejs控制台中收到此错误“编译原始查询时检测到未定义的绑定”错误和“预期1个绑定,看到2”。

如何使用第二个绑定参数?

有效的代码:

knex.with('with_alias', knex.raw('select * from "lyrics" where "for_id" = "1" and "var" = ?', var)).select('*').from('with_alias')

Run Code Online (Sandbox Code Playgroud)

我也尝试过但没有成功

knex.with('with_alias', knex.raw('select * from "lyrics" where "for_id" = ? and "var" = ?', var1, var2)).select('*').from('with_alias')

Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,抱歉英语不好!

小智 6

尝试在数组中传递两个变量:

knex.with('with_alias', knex.raw('select * from "lyrics" where "for_id" = ? and "var" = ?', [var1, var2])).select('*').from('with_alias')
Run Code Online (Sandbox Code Playgroud)

它应该有效。