查询方法中的返回无效。 console.log(result)向我显示正确的值。 getFirstColumnName()中的console.log()应该从查询中获取结果。
怎么了?
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "spacerowa44",
database: "m3pilot"
});
export class mySQL{
connect(){
con.connect(function(err) {
if (err) throw err;
console.log("database connected...");
});
}
private getFirstColumnName(table) : any{
var sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = '"+table+"' AND ORDINAL_POSITION = 1;";
var result = this.query(sql);
console.log();
setTimeout(() => {
console.log("1");
console.log(this.query(sql) );
return "conditionId";//result[0].COLUMN_NAME;
},200);
}
private query(sql) : any{
con.query(sql, function (err, result, fields) {
if (err) throw err;
console.log(result);
return result;
});
}
}