Search code examples
node.jspostgresqlnode-postgres

Postgres syntax in node-postgres for function call


Here is how I call postgres from my node.js app using express

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);
    })
});

And within my db-pg/index.js file (not including the detail of the pool setup):

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

I am getting the following error from postgreSQL :

syntax error at or near ","

The query I am trying to execute is :

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

What is wrong in my syntax ?


Solution

  • Just turning Chris White's comment into an answer:

    node-postgres recommends to use parameterized-queries for passing parameters.

    Here is the correct syntax for the SQL (all the rest looks fine):

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