Home > Enterprise >  Getting error trying to insert query from nodejs to postgresql database
Getting error trying to insert query from nodejs to postgresql database

Time:12-06

I am trying to get a password entered from my web application, hash it and send it to my database. Right now all is done in localhost.

I have been following a tutorial for the most part, the main difference being their database is mysql while I am using postgresql which I'm assuming is why I am having an error.

It is breaking down when I call db.query.

db.query(
  "INSERT INTO passwords (user_id, secure_password, iv, created_by) VALUES(?,?,?,?);",
  [1058083062,hashedPassword.password, hashedPassword.iv, 1058083062],
  (err,result) => {
      if (err) {
          console.log(err);
      } else {
          res.send("Success");
      }
  }

)

This is what db looks like:

const pg = require('pg');
const secret = require('./secret');

const db = new pg.Pool({
  user: secret.db_user,
  password: secret.db_password,
  host: secret.db_host,
  port: secret.db_port,
  database: secret.db_database,
});

module.exports = db;

I am getting this error:

error: syntax error at or near ","

with err showing:

{"length":90,"name":"error","severity":"ERROR","code":"42601","position":"74","file":"scan.l","line":"1180","routine":"scanner_yyerror"}

CodePudding user response:

Try

db.query(`INSERT INTO passwords (user_id, secure_password, iv, created_by) VALUES($1,$2,$3,$4)`,
  [1058083062,hashedPassword.password, hashedPassword.iv, 1058083062],
  (err,result) => {
      if (err) {
          console.log(err);
      } else {
          res.send("Success");
      }
  }
)

Notice we changed the ?,?,?,? to $1,$2,$3,$4 for param query

  • Related