dll dll - 2 months ago 20
Node.js Question

How return callback from a query made with node-oracledb?

The following code is the same code used in the node-oracledb GitHub examples, called select1.js. I just modified it a little bit.

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);
}
});
}
}
}


This can be used as follow:

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


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

How can I return a value that I get in the callback function?

I tried to add a variable X at the beginning, like this:

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;
}
}


But this is still undefined. How can I achieve this ?

Answer

It's running in async nature. You can resolve it with callback or promises. You can't get value like this.

pass the callback and return with callback

module.exports = function(callback) {//pass callback function and return with this
  var oracledb = require('oracledb');
  var dbConfig = require('./dbconfig.js');

  this.queryDB = function(query,callback) {
    oracledb.getConnection({
      user: dbConfig.user,
      password: dbConfig.password,
      connectString: dbConfig.connectString
    }, function(err, connection) {
      if (err) {
        console.error(err.message);
        return callback(err);
      }
      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 callback(null, result.rows)
      });
    });

    function doRelease(connection) {
      connection.release(function(err) {
        if (err) {
          console.error(err.message);
          return callback(err);
        }
      });
    }
  };
};