I am trying to query oracledb from nodejs. Below is the code that i use for querying
exports.simpleExecute = function(query,bindParams, options,callback) {
try {
pool.getConnection(function(err, connection) {
if (err) {
console.log(err);
}
connection.execute(query,bindParams, options,function(err, data) {
console.log(data);
if (err) {
console.log(err);
response.send({})
}
callback(null, data);
})
})
} catch (err) {
callback(err, null);
}
}
Below is the code that is making the request:
database.simpleExecute(query1,{},{outFormat: database.OBJECT},function(err, data1) {
// console.log(data2);
if (err) {
console.log(err);
response.send({});
}
var percentChange = ((data1.rows[0].COUNT - data1.rows[0].COUNT) / data2.rows[0].COUNT) * 100;
var data = [data1.rows[0].COUNT, percentChange];
response.send(data);
});
where query1 is : "SELECT count(distinct user_id) count_value FROM chatlog where trunc(timestamp) between to_date('2017-09-09','YYYY-MM-DD') and to_date('2017-10-08','YYYY-MM-DD')"
The problem is that the data1.rows parameter instead of coming as array of object is coming just as an array. Previously i tried some another method for connecting and querying taken from https://jsao.io/2015/03/making-a-wrapper-module-for-the-node-js-driver-for-oracle-database/ and things seems to be working well in that case.I was also getting the name of the parameter in data1.rows .The output that i am getting when i print the data1 is:
{ rows: [ [ 1 ] ],
resultSet: undefined,
outBinds: undefined,
rowsAffected: undefined,
metaData: [ { name: 'COUNT' } ] }
There are a couple of problems with your simpleExecute
function. The first is that you're wrapping everything in a try/catch block. However, you can't catch exceptions that occur during async operations like getConnection
and execute
.
The next problem is that you're not releasing the connection back to the pool after your done using it.
Finally, simpleExecute
has a reference to response.send({})
, which it shouldn't as the calling function is handling that.
Here's an example of how you could write this:
const oracledb = require('oracledb');
const config = require('./dbConfig.js');
let pool;
// Callback style simpleExecute
function simpleExecute(query, bindParams, options, callback) {
pool.getConnection(function(err, conn) {
if (err) {
callback(err);
return
}
conn.execute(query, bindParams, options, function(err, result) {
if (err) {
callback(err);
} else {
callback(null, result);
}
conn.close(function(err) {
if (err) {
console.log('error closing conn', err);
}
});
});
});
}
// Example of using simpleExecute after creating the pool
oracledb.createPool(config, function(err, p) {
if (err) {
throw err;
}
pool = p;
simpleExecute(
'select count(*) cnt from dual',
{},
{
outFormat: oracledb.OBJECT
},
function(err, result) {
if (err) {
console.log(err);
return;
}
console.log(result.rows); // [ { CNT: 1 } ]
}
);
});
You might find this series on async patterns useful: https://jsao.io/2017/06/how-to-get-use-and-close-a-db-connection-using-various-async-patterns/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.