【发布时间】:2019-04-19 12:40:36
【问题描述】:
我有以下代码:
pool.getConnection()
.then(conn => {
return conn.query("SELECT table_name FROM information_schema.tables WHERE TABLE_SCHEMA='SquadgoalsDB';")
.then((rows)=>{
for(let i=0; i< rows.length;i++){
tableNames.push(rows[i].table_name)
}
tableNames = []
if(tableNames.length == 0){
throw new Error("NO_TABLES_FOUND")
}
return conn.query("select DISTINCT(column_name) from information_schema.columns WHERE TABLE_SCHEMA='SquadgoalsDB'")
})
.then((rows)=>{
for(let i=0; i< rows.length;i++){
columnNames.push(rows[i].column_name)
}
if(columnNames.length == 0){
throw new Error("NO_COLUMNS_FOUND")
}
conn.end()
console.log("MariaDB connection works")
})
.catch((err) =>{
throw err;
conn.end()
})
}).catch(err =>{
console.log("not connected to mariadb due to error: " + err);
});
module.exports.tableNames = tableNames; //always empty
module.exports.columnNames = columnNames; // always empty
我想在我的数据库中搜索所有表名和列名。在此之后,我有更多的服务器启动工作,我想导出上面显示的两个数组,但它们总是空的,因为我们不等待查询?如何等待(可能使用 async/await)上述代码完成,然后继续导出和其他内容?
感谢您的帮助
【问题讨论】:
标签: node.js promise async-await mariadb