Search code examples
protractornode-oracledb

Protractor with oracledb, after fetching value from oracle db unable to access the valu from other function


i'm trying to write reusable function for fetch the value from oracledb and assign that value where ever required.

My sample code is below. //getTtalRwCnt.js

'use strict';
var db_con = require('./oracleConnectPage.js');
// var db = new db_con();
var OracelQP = db_con.getOracleQP_details();
var oracledb = require('oracledb');
var tmpRwCnt;

var getTtalRwCnt = function () {
    oracledb.outFormat = oracledb.ARRAY;
    var query = "SELECT TESTCASEID FROM EXECUTE_TESTCASE";
    oracledb.getConnection(OracelQP, function (err, connection) {
        if (err) {
            console.error(err.message); return;
        }
        connection.execute(query, function (err, result) {
            if (err) {
                console.error(err.message); 
                connection.close();
                return;
            }
            console.log("result----->" + result.rows.length);
            tmpRwCnt = result.rows.length;
            return tmpRwCnt;
        })
    })
}
module.exports.getTtalRwCnt = getTtalRwCnt();

//spec.js

    'use strict';
var rwCnt = require('../DBSample/DB/getTtalRwCnt.js');

describe('Get Total number of Rows from DB', function () {
  it('Get Rw count',function(){
    console.log(rwCnt.getTtalRwCnt());
  });
});

Solution

  • There are two issue in your code:

    1) export wrong

    module.exports.getTtalRwCnt = getTtalRwCnt();
    

    should be

    module.exports.getTtalRwCnt = getTtalRwCnt;
    

    2) tmpRwCnt is the return value of callback function, not the return value of function getTtalRwCnt, additionally you not have return for function getTtalRwCnt.

    To make value in callback function can be used outside callback, you can choose to use Promise solution.

    'use strict';
    var db_con = require('./oracleConnectPage.js');
    // var db = new db_con();
    var OracelQP = db_con.getOracleQP_details();
    var oracledb = require('oracledb');
    
    var getTtalRwCnt = function () {
        return new Promise(function(resolve, reject){
            oracledb.outFormat = oracledb.ARRAY;
            var query = "SELECT TESTCASEID FROM EXECUTE_TESTCASE";
            oracledb.getConnection(OracelQP, function (err, connection) {
                if (err) {
                    console.error(err.message);
                    reject(err);
                }
                connection.execute(query, function (err, result) {
                    if (err) {
                        console.error(err.message); 
                        connection.close();
                        reject(err);
                    }
                    rwCnt = result.rows.length;
                    console.log("result----->" + rwCnt);
                    resolve(rwCnt);
                })
            })
        });
    
    };
    
    module.exports = getTtalRwCnt;
    

    //spec.js

    'use strict';
    var getTtalRwCnt = require('../DBSample/DB/getTtalRwCnt.js');
    
    describe('Get Total number of Rows from DB', function () {
      it('Get Rw count',function(){
        var totalRw = getTtalRwCnt();
    
        // If you use Jasmine or Chai
        totalRw.then(function(total){
            console.log('total row: ' + total);
            expect(total).toEqual(10);
        });
    
        // if you use Jasmine 
        expect(totalRw).toEqual(10);
    
        // if you use Chai and Chai-as-promise
        expect(totalRw).to.eventually.to.equal(10);
      });
    });