How return callback from a query made with node-oracledb?
<p>The following code is the same code used in the node-oracledb GitHub examples, called <a href="https://github.com/oracle/node-oracledb/blob/master/examples/select1.js" rel="nofollow" title="select1.js">select1.js</a>. I just modified it a little bit.</p>

<pre><code>module.exports = function()  {
    var oracledb = require('oracledb');
    var dbConfig = require('./dbconfig.js');

    this.queryDB = function (query) {
        oracledb.getConnection({
            user            : dbConfig.user,
            password        : dbConfig.password,
            connectString   : dbConfig.connectString
        }, function(err, connection) {
            if (err) {
                console.error(err.message);
                return;
            }
            connection.execute(query, function(err, result) {
                if (err) {
                    console.error(err.message);
                    doRelease(connection);
                    return;
                }
                console.log(result.metaData);
                console.log(result.rows);
                doRelease(connection);
                return result.rows
            });
        });

        function doRelease(connection) {
            connection.release(function(err) {
                if (err) {
                    console.error(err.message);
                }
            });
        }
    }
}
</code></pre>

<p>This can be used as follow:</p>

<pre><code>require('./dbquery.js')();
console.log(queryDB("SELECT * FROM users"));
</code></pre>

<p>I expected to see the same 2D matrix (representing the table) as on line "<strong>console.log(result.rows);</strong>". But the "<strong>console.log(queryDB("SELECT * FROM users"));</strong>" returns "<strong>undefined</strong>". </p>

<p>How can I return a value that I get in the callback function? </p>

<p>I tried to add a variable X at the beginning, like this: </p>

<pre><code>module.exports = function()  {
    var oracledb = require('oracledb');
    var dbConfig = require('./dbconfig.js');

    this.queryDB = function (query) {
        var X;
        oracledb.getConnection({
            user            : dbConfig.user,
            password        : dbConfig.password,
            connectString   : dbConfig.connectString
        }, function(err, connection) {
            if (err) {
                console.error(err.message);
                return;
            }
            connection.execute(query, function(err, result) {
                if (err) {
                    console.error(err.message);
                    doRelease(connection);
                    return;
                }
                console.log(result.metaData);
                console.log(result.rows);
                doRelease(connection);
                X = result.rows
            });
        });

        function doRelease(connection) {
            connection.release(function(err) {
                if (err) {
                    console.error(err.message);
                }
            });
        }
        return X;
    }
}
</code></pre>

<p>But this is still undefined. How can I achieve this ? </p>