1

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 ?

1

2 Answers 2

2

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)"
Sign up to request clarification or add additional context in comments.

Comments

0

It should be like below.

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

1 Comment

wish it were that simple. typo fixed in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.