Search code examples
node.jsoraclenode-oracledb

Connection to oracle database via nodeJS


I'm trying to establish an Oracle connection using NodeJS but while trying to connect i am receiving below error.

Error:  Error: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help
Node-oracledb installation instructions: https://oracle.github.io/node-oracledb/INSTALL.html
You must have 64-bit Oracle client libraries in your PATH environment variable.
If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from
http://www.oracle.com/technetwork/topics/winx64soft-089540.html
A Microsoft Visual Studio Redistributable suitable for your Oracle client library version must be available.

    at OracleDb.getConnection (C:\NodeCon\node_modules\oracledb\lib\oracledb.js:270:10)
    at C:\NodeCon\node_modules\oracledb\lib\util.js:180:16
    at new Promise (<anonymous>)
    at OracleDb.getConnection (C:\NodeCon\node_modules\oracledb\lib\util.js:168:14)
    at C:\NodeCon\server.js:41:32
    at Object.<anonymous> (C:\NodeCon\server.js:58:3)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14) {
  errorNum: 0,
  offset: 0

I've downloaded and installed 64 bit Oracle Client Library but still below error appears.

Using Visual Studio Code v.1.36 as my editor.

NodeJS code i'm using is as below :

let connection;
var oracledb = require('oracledb');

(async function(){

    try{

        connection = await oracledb.getConnection({

            user: 'Username',
            password: 'Password',
            connectString: 'hostname:portname/servicename'
        });



        console.log("Successfully connected");
    } catch(err){

        console.log("NOT connected");
    }finally{

        if(connection){
            try{
                await connection.close();
            }catch(err){
                console.log("Errror");
            }
        }
    }
})()

Any help will be much appreciated. Thanks.


Solution

  • This got resolved by following code:

    oracledb.getConnection({
    
                user: 'username',
                password: 'Password',
                connectString: 'hostname:portname/servicename'
    
                },
    
    function(err, connection) {
                if (err) throw err;
    
    
             connection.execute(
    
            //Your database query here.
    
    
    //below code if you want to fetch data from database and show it on terminal
     function(err,results){
                     var metaData = {};
                     var rows = {};
                     console.log("error")
                     if (err){
                         throw err
                          }
                          metaData.name1 = results.metaData[0].name;      
                          metaData.name2 = results.metaData[1].name;
    
    
                     rows.row1 = results.rows[0][0];
                     rows.row2 = results.rows[0][1];
                     rows.row3 = results.rows[0][2];
                     console.log(metaData.name1+" : "+rows.row1)
                     console.log(metaData.name2+" : "+rows.row2)
    
            console.log("Successfully connected");
    
             );
    
          }
    
    

    And if you're connecting via VPN make sure to connect to the server first then run above.