I am trying to run simple SQL statements using Oracledb npm for Nodejs. This has worked in the past with no issue but suddenly I am getting this error in a different environment. Possibly due to aliases? I am not sure.
The goal of the line causing the issue is to close out the connection once it completes or fails. I found the logic directly in the docs from Oracle found here: http://oracle.github.io/node-oracledb/doc/api.html#connectionclose
Really would appreciate any help!
const oracledb = require('oracledb');
const async = require('async');
const {getAwsSecret} = require('./awssecret');
var alias = "";
async function initialize() {
// Set Oracle Pool Settings
let hrPool = {
poolMin: 10,
poolMax: 10,
poolIncrement: 0
}
var secret = await getAwsSecret('MAIN');
const secretJSON = JSON.parse(secret);
hrPool.user = secretJSON[process.env.CURENV + 'username'];
hrPool.password = secretJSON[process.env.CURENV + 'password'];
hrPool.connectString = secretJSON[process.env.CURENV + 'host'] + '/' + secretJSON[process.env.CURENV + 'dbname'];
hrPool.poolAlias = secretJSON.alias;
alias = secretJSON.alias;
try {
await oracledb.createPool(hrPool);
} catch (err) {
console.log(err);
}
}
module.exports.initialize = initialize;
async function close() {
await oracledb.getPool().close();
}
module.exports.close = close;
async function simpleExecute(statement, binds = [],clientdetails = [], opts = {}) {
let conn;
opts.outFormat = oracledb.OBJECT;
opts.autoCommit = true;
try {
// Get Connection
conn = await oracledb.getConnection(alias);
// Run Query
const result = await conn.execute(statement, binds, opts);
///////////// POST EXECUTION HANDLING /////////////
if (conn) { // conn assignment worked, need to close
try {
// Close Connection
await conn.close();
// Return Result
return result;
} catch (err) {
console.log(err);
}
}
///////////// POST EXECUTION HANDLING /////////////
} catch (err) {
await conn.close();
}
}
module.exports.simpleExecute = simpleExecute;
Error:
TypeError: Cannot read property 'close' of undefined 2021-04-23T14:36:51.269-04:00 at
Object.simpleExecute (/usr/src/app/services/database.js:59:18)