连接未在 oracledb 中定义
Connection is not defined in oracledb
我正在使用 oracledb cen node.js 模块,当建立数据库连接以创建 select 时,它 returns 数据但也出现此错误:
(node:1) UnhandledPromiseRejectionWarning: ReferenceError: connection is not defined
at Object.getTest (/home/src/storage/oracleDb.js:29:9)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
(rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我这样查询:
try {
await oracledb.getConnection(config.db)
.then(function (conn) {
return conn.execute(querys.queryTest());
}, function(err) {
console.log(err);
})
.then(function (result) {
console.log('Query executed');
console.log(result.rows[0]);
}, function(err) {
console.log(err);
})
.catch(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
您可以尝试将连接添加到在 try-catch
块外声明的变量,如下所示:
let connection;
try {
await oracledb.getConnection(config.db)
.then(function (conn) {
// this is where you assign the connection value to a variable
connection = conn;
return conn.execute(querys.queryTest());
}, function(err) {
console.log(err);
})
.then(function (result) {
console.log('Query executed');
console.log(result.rows[0]);
}, function(err) {
console.log(err);
})
.catch(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
} finally {
// this if should be fine now
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
我建议阅读 javascript 中有关范围界定的内容,它可能会帮助您解决未来的问题。这是 link:https://www.w3schools.com/js/js_scope.asp
如果您可以使用 await
,那么您就处于 async
函数中。如果您在 async
函数中,为什么要使用承诺链?
下面是使用 Promises 的此类代码:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(function(resolve, reject) {
let conn; // Declared here for scoping purposes.
oracledb
.getConnection()
.then(function(c) {
console.log('Connected to database');
conn = c;
return conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
})
.then(
function(result) {
console.log('Query executed');
resolve(result.rows[0]);
},
function(err) {
console.log('Error occurred', err);
reject(err);
}
)
.then(function() {
if (conn) {
// If conn assignment worked, need to close.
return conn.close();
}
})
.then(function() {
console.log('Connection closed');
})
.catch(function(err) {
// If error during close, just log.
console.log('Error closing connection', err);
});
});
}
module.exports.getEmployee = getEmployee;
下面是 async/await 的样子:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(async function(resolve, reject) {
let conn; // Declared here for scoping purposes.
try {
conn = await oracledb.getConnection();
console.log('Connected to database');
let result = await conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
console.log('Query executed');
resolve(result.rows[0]);
} catch (err) {
console.log('Error occurred', err);
reject(err);
} finally {
// If conn assignment worked, need to close.
if (conn) {
try {
await conn.close();
console.log('Connection closed');
} catch (err) {
console.log('Error closing connection', err);
}
}
}
});
}
module.exports.getEmployee = getEmployee;
有关详细信息,请参阅本系列:
https://jsao.io/2017/06/how-to-get-use-and-close-a-db-connection-using-various-async-patterns/
我正在使用 oracledb cen node.js 模块,当建立数据库连接以创建 select 时,它 returns 数据但也出现此错误:
(node:1) UnhandledPromiseRejectionWarning: ReferenceError: connection is not defined
at Object.getTest (/home/src/storage/oracleDb.js:29:9)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
(rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我这样查询:
try {
await oracledb.getConnection(config.db)
.then(function (conn) {
return conn.execute(querys.queryTest());
}, function(err) {
console.log(err);
})
.then(function (result) {
console.log('Query executed');
console.log(result.rows[0]);
}, function(err) {
console.log(err);
})
.catch(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
您可以尝试将连接添加到在 try-catch
块外声明的变量,如下所示:
let connection;
try {
await oracledb.getConnection(config.db)
.then(function (conn) {
// this is where you assign the connection value to a variable
connection = conn;
return conn.execute(querys.queryTest());
}, function(err) {
console.log(err);
})
.then(function (result) {
console.log('Query executed');
console.log(result.rows[0]);
}, function(err) {
console.log(err);
})
.catch(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
} finally {
// this if should be fine now
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
我建议阅读 javascript 中有关范围界定的内容,它可能会帮助您解决未来的问题。这是 link:https://www.w3schools.com/js/js_scope.asp
如果您可以使用 await
,那么您就处于 async
函数中。如果您在 async
函数中,为什么要使用承诺链?
下面是使用 Promises 的此类代码:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(function(resolve, reject) {
let conn; // Declared here for scoping purposes.
oracledb
.getConnection()
.then(function(c) {
console.log('Connected to database');
conn = c;
return conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
})
.then(
function(result) {
console.log('Query executed');
resolve(result.rows[0]);
},
function(err) {
console.log('Error occurred', err);
reject(err);
}
)
.then(function() {
if (conn) {
// If conn assignment worked, need to close.
return conn.close();
}
})
.then(function() {
console.log('Connection closed');
})
.catch(function(err) {
// If error during close, just log.
console.log('Error closing connection', err);
});
});
}
module.exports.getEmployee = getEmployee;
下面是 async/await 的样子:
const oracledb = require('oracledb');
function getEmployee(empId) {
return new Promise(async function(resolve, reject) {
let conn; // Declared here for scoping purposes.
try {
conn = await oracledb.getConnection();
console.log('Connected to database');
let result = await conn.execute(
`select *
from employees
where employee_id = :emp_id`,
[empId],
{
outFormat: oracledb.OBJECT
}
);
console.log('Query executed');
resolve(result.rows[0]);
} catch (err) {
console.log('Error occurred', err);
reject(err);
} finally {
// If conn assignment worked, need to close.
if (conn) {
try {
await conn.close();
console.log('Connection closed');
} catch (err) {
console.log('Error closing connection', err);
}
}
}
});
}
module.exports.getEmployee = getEmployee;
有关详细信息,请参阅本系列: https://jsao.io/2017/06/how-to-get-use-and-close-a-db-connection-using-various-async-patterns/