我在Typescript中有一个对象MYSQLCredentials,它包含登录MySQL数据库的凭据,该数据库被发送到MySQL对象的构造函数中,该对象实例化与MySQL服务器的连接。我无法让构造函数从凭证对象中读取属性。
//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);
答案 0 :(得分:1)
您错过了new
,以实例化新的MySQLCredentials
课程:
var credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');