【问题标题】:Basic NodeJS/PostgreSQL program is outputing error: bind message supplies 1 parameters, but prepared statement "" requires 0基本 NodeJS/PostgreSQL 程序输出错误:绑定消息提供 1 个参数,但准备好的语句“”需要 0
【发布时间】:2020-06-24 18:57:38
【问题描述】:

我正在编写应该连接到 Postgres 数据库并执行查询的 node.js 代码。

问题是我执行脚本时出错:bind message supplies 1 parameters, but prepared statement "" requires 0

我不明白发生了什么。有人可以帮我吗?

完整脚本:

const { Client } = require('pg');

let connectionString = 'postgresql://ezert:<myPassword>@localhost:5432/mydb';

const client = new Client({
    connectionString: connectionString
});

client.connect();

client.query('select * from my_table', [1], function (err, result) {

    if (err) {

        console.log('[ERROR]:\n' + err);
        return;

    }

    console.log('[RESULT]:\n' + result.rows);
    return;

});

【问题讨论】:

    标签: node.js postgresql


    【解决方案1】:

    来自documentation

    查询函数recibe可选参数

    client.query(
      text: string,
      values?: Array<mixed>,
      callback: (err: Error, result: Result) => void
    ) => void
    

    您的查询select * from my_table 没有收到任何参数,但您提供了一个[1]

    选项1:删除参数

    client.query('select * from my_table', function (err, result) {
       // your code here
    });
    

    选项 2:在查询中使用参数

    例如:

    client.query('select * from my_table where columnName=$1', [1], function (err, result) {
       // your code here
    });
    

    (columnName 应该设置为 my_table 的列)

    【讨论】:

      【解决方案2】:

      '从 my_table 中选择 *',[1]

      您的查询不使用任何参数(查询文本中没有 $1、$2 等占位符),但正在尝试将包含 1 的单个参数填充到其中。

      如果您的查询不带参数,请不要给它任何参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 2013-08-09
        • 1970-01-01
        • 2014-11-30
        • 1970-01-01
        • 2017-05-14
        • 2021-06-21
        • 2021-08-01
        相关资源
        最近更新 更多