无法将对象传递给Typescript中的构造函数
我在Typescript中有一个对象MYSQLCredentials,它保存登录MySQL数据库的凭据,该数据库被发送到MySQL对象的构造函数中,该对象实例化与MySQL服务器的连接。我无法使构造函数从凭据对象读取属性无法将对象传递给Typescript中的构造函数,typescript,Typescript,我在Typescript中有一个对象MYSQLCredentials,它保存登录MySQL数据库的凭据,该数据库被发送到MySQL对象的构造函数中,该对象实例化与MySQL服务器的连接。我无法使构造函数从凭据对象读取属性 //MySQL connection instantiation object constructor(cred:MySQLCredentials){ this.mysqlConnection = mysql.createConnection({
//MySQL connection instantiation object
constructor(cred:MySQLCredentials){
this.mysqlConnection = mysql.createConnection({
host : cred.getHost(), //This is the line with the error
user : cred.getUser(),
password : cred.getPassword()
});
}
//Credentials object
export class MySQLCredentials implements Credentials{
host:string;
user:string;
password:string;
constructor(host:string, user:string, password:string){
console.log("STARTING SQL");
this.host = host;
this.user = user;
this.password = password;
}
public getHost():string{
return this.host;
}
public getUser():string{
return this.user;
}
public getPassword():string{
return this.password;
}
}
//Error: TypeError: Cannot call method 'getHost' of undefined
以下是运行时:
var credentials = mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');
var sqlConnector = new mysql.Mysql(credentials);
您刚刚错过了一个要实例化一个新的
MySQLCredentials
类的new
:
var credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');
这意味着在运行时,您传递的是
undefined
,而不是MySQLCredentials
@Anzeo的实例。我已经更新了问题以显示上述运行时