Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用knex重写原始sql查询?_Javascript_Postgresql_Knex.js_Next - Fatal编程技术网

Javascript 如何使用knex重写原始sql查询?

Javascript 如何使用knex重写原始sql查询?,javascript,postgresql,knex.js,next,Javascript,Postgresql,Knex.js,Next,我刚刚开始开发,这是我第一次使用knex 问题: 我有一个原始SQL查询,可以正常工作。现在,我正在尝试使用knex进行此查询。为了了解一切工作原理,我想: 使用knex.raw重写查询 使用knex查询生成器重写查询 有人能帮我吗?顺便说一句,我正在使用Postgres和Next.js。 通过运行下面的代码,我得到了“UnhandledPromisejectionWarning:错误:应为1个绑定,锯为0”。我不知道问题是否出在这里: typeof req.query.word==='stri

我刚刚开始开发,这是我第一次使用knex

问题: 我有一个原始SQL查询,可以正常工作。现在,我正在尝试使用knex进行此查询。为了了解一切工作原理,我想:

  • 使用knex.raw重写查询
  • 使用knex查询生成器重写查询
  • 有人能帮我吗?顺便说一句,我正在使用Postgres和Next.js。 通过运行下面的代码,我得到了“UnhandledPromisejectionWarning:错误:应为1个绑定,锯为0”。我不知道问题是否出在这里:

    typeof req.query.word==='string'?[req.query.word]:req.query.word)

    。。。所以我已经试着重写它(使用[]),但没有成功。代码如下:

    const getTranslation=(请求、恢复)=>{
    常数参数=
    typeof req.query.word==='string'
    ?req.query.word
    :req.query.word.map((\ux,index)=>`$${index+1}`);
    console.log(req.query.word);
    生的(
    `从“单词”所在的“词典”中选择“翻译”、“单词”(${
    typeof req.query.word=='string'?'($1)':params.join(','))
    })`,
    typeof req.query.word=='string'?[req.query.word]:req.query.word)
    .然后((错误、结果)=>{
    const wordArray=typeof req.query.word=='string'?[req.query.word]:req.query.word;
    如果(错误){
    投掷误差;
    } 
    const wordOrder=req.query.word;
    result.rows.sort((第1行,第2行)=>{
    返回wordOrder.indexOf(行1.Words)-wordOrder.indexOf(行2.Words);
    });
    res.status(200.json)(result.rows);
    }
    );
    
    };请记住,我实际上并不了解Knex

    但是当我在下面的链接中查看问题的一些答案时,我注意到它们使用了“Knex.with()

    因此,他们似乎将其与“with_alias”绑定在一起

    他们还指定您应该尝试在数组中传递变量


    我希望这对您有所帮助。

    您的查询应该如下所示:

    const results=wait knex('Dictionary'))
    .列([“翻译”、“单词])
    其中('Words',req.query.word);//假设'req.query.word'是带有字符串/数字的数组
    
    Thx用于共享链接。是的,我尝试在数组中传递值(请参见我的问题),但没有成功。我还假设错误可能在这里的某个地方。据我所知,knex.with用于提供别名-因此,我认为这不是问题所在。Thx。我不得不对……做些小改动。但它是有效的。
    knex.with('with_alias', knex.raw('select * from "lyrics" where "for_id" = ? and "var" = ?', [var1, var2])).select('*').from('with_alias')