nodejsmysql返回空结果

nodejsmysql返回空结果,mysql,node.js,Mysql,Node.js,我有查询数据并以json格式返回结果的api。如果我在api.get内部调用查询并设置res.send(行),它工作正常,但是我需要在不同的方法中调用相同的方法,所以我想我可以在外部编写它,并在需要时调用该方法。但是当它在外部时,结果返回空 var customerRows[] app.get('/customers', function(req, res) { getCustomers(); res.json({ customers : customerRows

我有查询数据并以json格式返回结果的api。如果我在api.get内部调用查询并设置res.send(行),它工作正常,但是我需要在不同的方法中调用相同的方法,所以我想我可以在外部编写它,并在需要时调用该方法。但是当它在外部时,结果返回空

var customerRows[]
app.get('/customers', function(req, res) {
    getCustomers();
    res.json({
        customers : customerRows
    });
});

function getCustomersQuery(callback) {
    var customersDataQuery = mysqlConnection.query('SELECT * from customer_info', function(err, rows, fields) {
        if (!err) {
            if (rows) {
                callback(null, rows);
            }
        } else {
            callback(err, null);
            console.log('Error while performing Query.');
        }
    });
}

function getCustomers() {
    getCustomersQuery(function(err, result) {
        if (err) {
            console.log(err);
        } else {
            customerRows.push(result);
            console.log(customerRows)//prints values
        }
    });
    console.log("Result : "+customerRows);//prints empty
}
我试图将结果设置为全局变量customerRows,但它返回空

使用此代码

app.get('/customers', function(req, res) {    
    getCustomersQuery(function(err, result) {
        if (err) {
            console.log(err);
        }        
        res.json({
            customers : result
        });
    });
});

function getCustomersQuery(callback) {
    var customersDataQuery = mysqlConnection.query('SELECT * from customer_info', function(err, rows, fields) {
        if (!err) {
            if (rows) {
                callback(null, rows);
            }
        } else {
            callback(err, null);
            console.log('Error while performing Query.');
        }
    });
}

也许你们应该知道异步函数的概念。。在执行mysql查询之前,将触发最后一个console.log。我将此标记为非答案,因为它不是答案-它只是一些代码。我看到过一些东西以更少的价格被移除,这是一个可以接受的答案吗?