我無法訪問函數中Typescript中的類中聲明的對象。這是我有:無法訪問Typescript中的對象
export class QUestions {
credentials:mysqlCredentials.MySQLCredentials;
sqlConnector:mysql.Mysql;
constructor() {
this.credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev', 'test');
this.sqlConnector = new mysql.Mysql(this.credentials);
}
addQuestion(req, res) {
var q = ...<Lot of code over here> ;
this.sqlConnector.query(q, null); //Error shown for this line
res.send();
}
}
Error:
TypeError: Cannot call method 'query' of undefined
如果如上所示即sqlConnector變量在打字稿類定義的代碼的結構,它引發錯誤。如果我將sqlConnector放置在Typescript類之外,它可以正常工作。我需要解決這個問題,因爲我需要類中的sqlConnector對象。
export class Questions {
credentials:mysqlCredentials.MySQLCredentials;
constructor() {
this.credentials = new mysqlCredentials.MySQLCredentials('192.168.249.139', 'dev', 'dev', 'test');
sqlConnector = new mysql.Mysql(this.credentials);
}
addQuestion(req, res) {
var q = ...<Lot of code over here> ;
sqlConnector.query(q, null); //Error shown for this line
res.send();
}
}
var sqlConnector;