EternallyCurious
EternallyCurious

Reputation: 2415

Unable to pass objects to constructors in Typescript

I have an object MYSQLCredentials in Typescript that holds the credentials for logging into a MySQL database which is sent into the constructor of a MySQL object that instantiates a connection with a MySQL Server. I am not able to get the constructor to read properties from the credentials object.

//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

Here's the runtime:

var credentials = mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');
var sqlConnector = new mysql.Mysql(credentials);

Upvotes: 0

Views: 599

Answers (1)

Fenton
Fenton

Reputation: 251262

You have just missed a new, to instantiate a new MySQLCredentials class:

var credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev');

Upvotes: 1

Related Questions