类型错误:ConnectionConfig 不是构造函数

时间:2021-02-10 13:39:45

标签: javascript node.js typescript

您好,我一直收到 TypeError 的错误:ConnectionConfig 不是第 22 行的构造函数,我不知道为什么。我正在使用 mysql 包。所以我希望有人能帮我解决这个问题,因为我不知道是什么原因造成的。谢谢。

//3rd party modules
import bcrypt from "bcrypt";
import mysql from "mysql";

//Interfaces
import { Sql } from "./interfaces/sql.interface";
import { SqlTable } from "./interfaces/table.interface";
import { User } from "./interfaces/user.interface";

//Classes
import LoginAuth from "./loginAuth";

class UserAuth {
  user: string;
  password: string;
  sqlconnection: Sql;
  table: SqlTable;
  hashedPass: string;
  registeredUser!: User;

  constructor(
    user: string,
    password: string,
    sqlconnection: Sql,
    table: SqlTable
  ) {
    this.user = user;
    this.password = password;
    this.sqlconnection = sqlconnection;
    this.table = table;
    this.hashedPass = "";
  }

  registerUser() {
    bcrypt.genSalt(10, (err, salt) => {
      bcrypt.hash(this.password, salt, (err, hash) => {
        this.hashedPass = hash;
        const connection = mysql.createConnection({
          host: this.sqlconnection.host,
          user: this.sqlconnection.user,
          password: this.sqlconnection.password,
          database: this.sqlconnection.database,
        });
        connection.connect();
        this.registeredUser = {
          username: this.user,
          password: this.hashedPass,
        };
        connection.query(
          `INSERT INTO ${this.table.table} SET ?`,
          this.registeredUser
        );
      });
    });
  }
}

export default UserAuth;

0 个答案:

没有答案