我正在尝试使用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'变量中。
反正是为了避免承诺和异步功能?有没有其他解决方案来实现这一目标?请提供有价值的解决方案和建议。期待样本片段。
你错误的实际原因
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 ] ]
似乎你在功能this
中以错误的方式使用getRwCnt()
。
请记住,JavaScript中的每个函数都有自己的
this
。
选项1将顶部this
分配给函数开头的另一个变量
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();
});
选项2使用ES6箭头功能
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();
});
我尝试了你的解决方案,但看起来像打字稿不等待电话等待me.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 ] ]