I am using the oracledb cen node.js module and when making a database connection to make a select, it returns the data but this error also appears:
(node:1) UnhandledPromiseRejectionWarning: ReferenceError: connection is not defined
at Object.getTest (/home/src/storage/oracleDb.js:29:9)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
(rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I do the query like this:
try {
await oracledb.getConnection(config.db)
.then(function (conn) {
return conn.execute(querys.queryTest());
}, function(err) {
console.log(err);
})
.then(function (result) {
console.log('Query executed');
console.log(result.rows[0]);
}, function(err) {
console.log(err);
})
.catch(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
If you can use await
, then you're in an async
function. If you're in an async
function, why are you using promise chains?
Here's what this type of code would look like with Promises:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(function(resolve, reject) {
let conn; // Declared here for scoping purposes.
oracledb
.getConnection()
.then(function(c) {
console.log('Connected to database');
conn = c;
return conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
})
.then(
function(result) {
console.log('Query executed');
resolve(result.rows[0]);
},
function(err) {
console.log('Error occurred', err);
reject(err);
}
)
.then(function() {
if (conn) {
// If conn assignment worked, need to close.
return conn.close();
}
})
.then(function() {
console.log('Connection closed');
})
.catch(function(err) {
// If error during close, just log.
console.log('Error closing connection', err);
});
});
}
module.exports.getEmployee = getEmployee;
And here's what it would look like with async/await:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(async function(resolve, reject) {
let conn; // Declared here for scoping purposes.
try {
conn = await oracledb.getConnection();
console.log('Connected to database');
let result = await conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
console.log('Query executed');
resolve(result.rows[0]);
} catch (err) {
console.log('Error occurred', err);
reject(err);
} finally {
// If conn assignment worked, need to close.
if (conn) {
try {
await conn.close();
console.log('Connection closed');
} catch (err) {
console.log('Error closing connection', err);
}
}
}
});
}
module.exports.getEmployee = getEmployee;
See this series for more info: https://jsao.io/2017/06/how-to-get-use-and-close-a-db-connection-using-various-async-patterns/