我在Typescript中有一个对象MYSQLCredentials,其中包含用于登录到MySQL数据库的凭据,该数据库发送到实例化与MySQL Server的连接的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);
您刚错过了new
要实例化新MySQLCredentials
课程的,请执行以下操作:
var credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句