Chapo
Chapo

Reputation: 2543

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 ?

Upvotes: 1

Views: 4792

Answers (2)

Urigo
Urigo

Reputation: 3185

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)"

Upvotes: 2

narayansharma91
narayansharma91

Reputation: 2353

It should be like below.

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

Upvotes: 0

Related Questions