我正在尝试使用 Oracledb npm for Nodejs 运行简单的 SQL 语句。这在过去没有问题,但突然我在不同的环境中遇到了这个错误。可能是因为别名?我不确定。
导致问题的线路的目标是在连接完成或失败后关闭连接。我直接在 Oracle 的文档中找到了逻辑:http: //oracle.github.io/node-oracledb/doc/api.html#connectionclose
真的很感激任何帮助!
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;
错误:
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)