我正在尝试使用Typescript ES6 Class模块实现oracle连接。
我安装了@ types / oracledb软件包以及oracledb软件包。使用Jasmin框架。
以下是我已实施的代码。
import * as oracledb from 'oracledb';
export class ConnectionDAO{
/**
* Connection Variable Declaration
*/
conn;
/**
* Result Variable Declaration
*/
result;
/**
*
* Creates an instance of CommercialDAO.
* To Initiate Connection and Make the connection utilized by @memberof CommercialDAO
* @memberof CommercialDAO
*/
constructor() {
this.conn = oracledb.getConnection({
user: "commercial",
password: "oracle",
connectString: "localhost/COMMERCIALDB"
});
}
public getRwCnt() {
return new Promise(async function(resolve, reject) {
try {
let result = this.conn.execute('SELECT TESTCASEID FROM EXECUTE_TESTCASE');
resolve(this.result.rows.length);
} catch (err) { // catches errors in getConnection and the query
reject(err);
}
this.conn.release();
});
}
}
错误:
TypeError: this.conn.execute is not a function
但是这个代码连接本身没有存储在'this.conn'变量中。
无论如何都要避免承诺和异步功能? 有没有其他解决方案来实现这一目标?请提供有价值的解决方案和建议。期待样本片段。
答案 0 :(得分:1)
错误的实际原因
TypeError: this.conn.execute is not a function
是因为this.conn很可能未定义。像这样添加支票。
public getRwCnt() {
if(this.conn === undefined){
console.log("The connection is not ready yet.");
return;
... // Rest of your function
}
但这只会突出显示您有问题,而不能告诉您原因。
原因是您的构造函数严格同步。 考虑具有一个等待构造函数完成的函数。
这是一个有效的版本:
import * as OracleDB from 'oracledb';
export class ConnectionDAO {
/**
* Connection Variable Declaration
*/
public conn: OracleDB.IConnection;
public connProm: OracleDB.IPromise<void>;
/**
* Result Variable Declaration
*/
result;
/**
*
* Creates an instance of CommercialDAO.
* To Initiate Connection and Make the connection utilized by @memberof CommercialDAO
* @memberof CommercialDAO
*/
constructor() {
this.connProm = OracleDB.getConnection({
user: "hr",
password: "hr",
connectString: "localhost/XEPDB1"
}).then(async (connection: OracleDB.IConnection) => {
console.log("Connection finally created in constructor")
this.conn = connection;
}).catch((err: any) => {
console.error(err.message);
});
console.log(" - Dumping Connection state in the end of the constructor", {conn: this.conn} , {connProm: this.connProm} );
}
public getRwCnt() {
let me = this;
return new Promise(async function (resolve, reject) {
try {
console.log(" - Dumping Connection state BEFORE waiting",{conn: me.conn} , {connProm: me.connProm} );
await me.connProm;
console.log(" - Dumping Connection state AFTER waiting",{connServerVersion: me.conn.oracleServerVersion } , {connProm: me.connProm} );
let result = await me.conn.execute('SELECT count(*) FROM employees');
resolve(result.rows);
} catch (err) { // catches errors in getConnection and the query
console.log("[Error] happened? - calling reject",err);
reject(err);
}
if(me.conn) // Only release it it if it actually is set
me.conn.release();
});
}
}
const d = new ConnectionDAO();
d.getRwCnt()
.then((result)=>{console.log("RowCount",result)})
.catch((err)=>{console.error("Promise rejected - ",err)})
console.log("Object constructor returned");
在ts-node上运行可以得到结果
- Dumping Connection state in the end of the constructor { conn: undefined } { connProm: Promise { <pending> } }
- Dumping Connection state BEFORE waiting { conn: undefined } { connProm: Promise { <pending> } }
Object constructor returned
Connection finally created in constructor
- Dumping Connection state AFTER waiting { connServerVersion: 1804000000 } { connProm: Promise { undefined } }
RowCount [ [ 107 ] ]
答案 1 :(得分:0)
似乎您在函数getRwCnt()
中以错误的方式使用this
。
请记住JavaScript中的每个函数都有自
this
。
选项1 将最高public getRwCnt() {
let me = this;
return new Promise(async function(resolve, reject) {
try {
let result = me.conn.execute('SELECT TESTCASEID FROM EXECUTE_TESTCASE');
resolve(this.result.rows.length);
} catch (err) { // catches errors in getConnection and the query
reject(err);
}
me.conn.release();
});
分配给函数开头的另一个变量
public getRwCnt() {
return new Promise(async (resolve, reject) => {
try {
let result = this.conn.execute('SELECT TESTCASEID FROM EXECUTE_TESTCASE');
resolve(this.result.rows.length);
} catch (err) { // catches errors in getConnection and the query
reject(err);
}
this.conn.release();
});
选项2 使用ES6箭头功能
SELECT *
FROM tablename
WHERE count = (SELECT MAX(count) FROM tablename)
答案 2 :(得分:0)
我尝试了您的解决方案,但看起来打字稿没有在等电话 等待我.connectionPromise; 另外,不确定连接是否成功。我低于输出。
Inside constructor
get Connection executed....
- Dumping Connection state in the end of the constructor { conn: undefined } { connProm: Promise { <pending> } }
Inside getRowNumbers function call....
Connection state BEFORE waiting: { conn: undefined } { connectionPromise: Promise { <pending> } }
Object constructor returned
Somehow below output lines in your code are missing for me.
Connection finally created in constructor
- Dumping Connection state AFTER waiting { connServerVersion: 1804000000 } { connProm: Promise { undefined } }
RowCount [ [ 107 ] ]