【问题标题】:Getting error trying to insert query from nodejs to postgresql database尝试将查询从 nodejs 插入到 postgresql 数据库时出错
【发布时间】:2022-01-11 07:17:40
【问题描述】:

我正在尝试从我的 Web 应用程序中获取输入的密码,对其进行哈希处理并将其发送到我的数据库。现在一切都在 localhost 中完成。

我大部分时间都在学习教程,主要区别在于他们的数据库是 mysql,而我使用的是 postgresql,我假设这就是我出错的原因。

当我调用 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");
      }
  }

)

这就是 db 的样子:

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;

我收到此错误:

error: syntax error at or near ","

显示错误:

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

【问题讨论】:

    标签: node.js postgresql


    【解决方案1】:

    试试

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

    请注意,我们将 ?,?,?,? 更改为 $1,$2,$3,$4 以进行参数查询

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多