简体   繁体   English

从nodejs查询oracle db时未获取参数名称

[英]Not getting parameter name on querying oracle db from nodejs

I am trying to query oracledb from nodejs.我正在尝试从 nodejs 查询 oracledb。 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')"其中 query1 是:“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.问题是 data1.rows 参数不是作为对象数组出现,而是作为一个数组出现。 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:以前我尝试了另一种方法来连接和查询https://jsao.io/2015/03/making-a-wrapper-module-for-the-node-js-driver-for-oracle-database/和东西在这种情况下似乎运行良好。我还在 data1.rows 中获取了参数的名称。当我打印 data1 时,我得到的输出是:

{ rows: [ [ 1 ] ],
resultSet: undefined,
outBinds: undefined,
rowsAffected: undefined,
metaData: [ { name: 'COUNT' } ] }

There are a couple of problems with your simpleExecute function.您的simpleExecute函数存在一些问题。 The first is that you're wrapping everything in a try/catch block.第一个是您将所有内容都包装在 try/catch 块中。 However, you can't catch exceptions that occur during async operations like getConnection and execute .但是,您无法捕获在getConnectionexecute等异步操作期间发生的异常。

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.最后, simpleExecute有一个对response.send({})的引用,因为调用函数正在处理它,所以它不应该引用。

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/你可能会发现这个关于异步模式的系列很有用: https : //jsao.io/2017/06/how-to-get-use-and-close-a-db-connection-using-various-async-patterns/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM