Dalton2
Dalton2

Reputation: 135

Parsing JSON result from node oracledb

I am using oracledb with node and fetching data from it asynchronously. For the sake of ease, I have implemented it using separate files like below -

config.js -

module.exports = {
  user          : "user",
  password      : "password",
  connectString : "*connstring*"  ,
  deliveredQuery: " SELECT COUNT (DISTINCT order_num) AS Cnt from orders where department = 'HR'
};

query2.js :

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

  this.queryDB = function(query,callback) {
    oracledb.getConnection({
      user: dbConfig.user,
      password: dbConfig.password,
      connectString: dbConfig.connectString,
      deliveredQuery: dbConfig.deliveredQuery    
    }, 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(JSON.parse(result.rows[0][0]));
        doRelease(connection);
        return callback(null, JSON.parse(result.rows[0][0]))
      });
    });

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

serv_ontime.js :

var dbConfig = require('./config.js');
var res = require('./query2.js')();
var onTime_query = dbConfig.onTime_query;

module.exports = queryDB(onTime_query, function(err, callback){  });

index.js :

var res = require('./serv_ontime.js');
console.log("The result is= "+  res);

Now, When I am doing - node index.js from my cmd then I am getting the output as [object Object]. I suppose it is because the call is happening asynchronously. But if you see in the file query2.js , I am returning the value after parsing(using JSON.parse) but still the value I am getting in the index.js file is not the parsed one. How can I parse the value in index.js? I have already tried JSON.parse but it doesn`t work.

Upvotes: 0

Views: 2235

Answers (2)

ponury-kostek
ponury-kostek

Reputation: 8060

In serv_ontime.js you are exporting result of queryDB witch indeed is undefined. Ty this:

serv_ontime.js

var dbConfig = require('./config.js');
var res = require('./query2.js')();
var onTime_query = dbConfig.onTime_query;

module.exports = function (callback) {
    queryDB(onTime_query, callback)
};

index.js

var serv_ontime = require('./serv_ontime.js');
serv_ontime(function(error, res) {
    console.log("The error is= ", error);
    console.log("The result is= ", res);
});

Upvotes: 2

user4532736
user4532736

Reputation:

You are getting output as [object Object] because you are doing + with a String('The result is= '), So js engine tries to convert the Object to a String. To view it as an Object, do log it separately or log with comma separated,

console.log('The result is= ', res); (or)
console.log(res); // or console.dir(res)

Or you can get String version of it, by doing JSON.stringify(res)

console.log('The result is= ', JSON.stringify(res));

Upvotes: 3

Related Questions