【问题标题】:NodeJS and MariaDB, wait for query resultNodeJS 和 MariaDB,等待查询结果
【发布时间】: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


    【解决方案1】:

    我相信你可以这样做:await 应该等待整个 Promise 链完成,并且它应该收到一个表示已解决或已拒绝的 Promise 的对象。 但请记住,导出也是 async 操作,因此它可能在函数完成之前发生。

    使用异步系统的最佳方式是使用回调。您需要导出一个回调分配方法来获取回调,并在异步执行时调用它。

    module.exports = pool.getConnection()
        .then( async (conn) => {
           return await 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);
        });
    

    在另一个文件中你可以这样做:

    (async function(){
    
      let foo = await require("./codeabove");
      console.log(foo);
    })();
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 2017-10-30
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多