NODEJS INSERT DATA INTO POSTGRESQL (ошибка: синтаксическая ошибка в или около $) - PullRequest
0 голосов
/ 09 ноября 2019

Я подготовил операторы в nodejs, чтобы вставить данные в базу данных с помощью posgresql. Я могу сказать, где я иду не так, но каждый раз я получаю эту ошибку (error: syntax error at or near "$") Пожалуйста, помогите.

class Employees {
  constructor(user) {
    this.client = require("../database");
    this.createTable();
    // this.dropTable();
    this.bcrypt = require("bcrypt");
    this.user = user;
  }

  createTable() {
    this.client.query(
      "CREATE TABLE IF NOT EXISTS employees(userId SERIAL,firstName varchar(255) NOT NULL,lastName varchar(255) NOT NULL,email varchar(255) NOT NULL,password varchar(255) NOT NULL,gender varchar(255) NOT NULL,jobRole varchar(255) NOT NULL,department varchar(255) NOT NULL,address varchar(255) NOT NULL,PRIMARY KEY (userId))",
      (err, res) => {
        console.log("ERROR ==> " + err);
        console.log("RESPONSE ==> " + res);
      }
    );
  }

  // dropTable() {
  //     this.client.query("DROP TABLE employees", (err, res)=>{
  //         console.log("ERROR ==> " + err);
  //         console.log("RESPONSE ==> " + res);
  //     })
  // }

  // create employees and save into the databse
  async save() {
    this.bcrypt.hash(this.user.password, 10).then(hash => {
      // let sql = "INSERT INTO Teamwork.employees(userId,firstName,lastName,email,password,gender,jobRole,department,address) VALUES($fn, $ln, $em, $pwd, $gen, $job,$dep, $addr)";
      let params = [
        "",
        this.user.firstName,
        this.user.lastName,
        this.user.email,
        this.user.password,
        this.user.gender,
        this.user.jobRole,
        this.user.department,
        this.user.address
      ];

      this.client.query(
        "INSERT into employees (userId, firstName, lastName, email, password, gender, jobRole, department, address) VALUES($id, $1, $2, $3, $4, $5, $6, $7, $8) RETURNING userId",
        params,
        (err, res) => {
          console.log("ERROR ==> " + err);
          console.log("RESPONSE ==> " + res);
        }
      );

      // .catch( (error)=>{
      //     console.log(error);
      // })

      // .finally( ()=>this.client.end())
    });
  }
}

module.exports = Employees;

Выше приведен мой фрагмент кода.

1 Ответ

1 голос
/ 09 ноября 2019

Удалите userID в операторе вставки, так как это поле помечено как последовательный, поэтому его не следует включать в оператор.

...