【问题标题】:Postgres syntax in node-postgres for function call用于函数调用的 node-postgres 中的 Postgres 语法
【发布时间】:2020-02-15 23:02:29
【问题描述】:

这是我使用 express 从我的 node.js 应用程序调用 postgres 的方式

const db_pg = require("./db-pg");
app.get('/pg/', (req,res,next) => {
    db_pg.query(req).then((body) => {
        res.send(body);
    }).catch((err) => {
        next(err);
    })
});

在我的db-pg/index.js 文件中(不包括pool 设置的详细信息):

module.exports = {
    query: (req) => {
        return pool.query(req);
    }
};

我从 postgreSQL 收到以下错误:

syntax error at or near ","

我要执行的查询是:

req = {
    text: "SELECT * from my_func(?,?,?)",
    values: ["the_name", 20190303, 20190620]
}

我的语法有什么问题?

【问题讨论】:

标签: node.js postgresql node-postgres


【解决方案1】:

只需将Chris White 的评论变成答案:

node-postgres 建议使用parameterized-queries 传递参数。

下面是正确的 SQL 语法(其余的看起来都很好):

"SELECT * FROM my_func($1, $2, $3)"

【讨论】:

    【解决方案2】:

    应该如下所示。

    req = {
        text: "SELECT * from my_func(?,?,?)",
        values: ["the_name", 20190303, 20190620]
    }
    

    【讨论】:

    • 希望就这么简单。问题中的错字已修复
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多