nodejs 8.6, MariaDB, MySQL2/promise 및 클래스를 사용하여 간단하다고 생각한 항목을 시도했습니다. 그러나이 작동하지 않습니다 : 나는이 프로그램을 실행하면NodeJS async/await class mysql/mariadb
const mysql = require('mysql2/promise');
class mySQLClass {
constructor() {
this.mysqlConn = null;
}
async initialize() {
try {
this.mysqlConn = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'myschema'
});
console.log('intialize complete - createConnection successful: ');
} catch (err) {
console.log('initialize failed: ' + err);
}
}
async showMeSomeData() {
try {
const [rows, fields] = await this.mysqlConn.execute('select * from information_schema.tables where table_schema = \'information_schema\'');
console.log('data: ' + rows);
} catch (err) {
console.log('showMeSomeData failed: ' + err);
}
}
}
const test = new mySQLClass();
test.initialize();
test.showMeSomeData();
, 그것은 실패 : 여기
은 간단한 예입니다 그래서showMeSomeData failed: TypeError: Cannot read property 'execute' of null
intialize complete - createConnection successful
, 그것은) (즉, 초기화를하지 않습니다 나타납니다 showMeSomeData()가 실행되기 전에 완료됩니다. 나는 이것이 올바르게 작동 할 수 있다고 생각했다.
나는 뭔가를 놓친가요? 더 좋은 방법이 있나요?
감사합니다.
감사합니다. 그것으로 문제가 해결되었습니다. 약속 또는 콜백을 사용하는 것과 비교하여 async/await를 사용하는 것의 가치에 대해 질문하게합니다. – nwrbs