我尝试使用节点js,express和OracleDB创建API。 我在server.js中的示例代码如下。 在命令行界面中创建api时出现以下错误,即Connection.connect不是函数。 请帮我解决一下this。
server.js
var express = require('express');
var oracledb = require('oracledb');
var app = express();
var dbConfig = require('./dbconfig.js');
// Get a non-pooled connection
var connection = oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
}
);
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
app.get("/",function(req,res){
connection.query('SELECT * from employees', function(err, rows, fields) {
connection.end();
if (!err) {
console.log('The solution is: ', rows);
} else {
console.log('Error while performing Query.');
}
});
});
app.listen(3000);
dbconfig.js
module.exports = {
user : "xxx",
password : "xxxx",
connectString : "localhost/XE"
}
答案 0 :(得分:0)
你应该像这样使用getConnection的回调函数。
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
}, function(err, connection) {
if (err) {
console.log(err)
} else {
// DO WHAT YOU NEED ...
}
}
)
您还应尝试使用关键字" await"来调用getConnection函数。像这样:
const connection = await oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
}
)
该行" connection.connect(...."似乎没有必要。
答案 1 :(得分:0)
这是因为getConnection不返回它返回promise的连接。您可以设置快速中间件以将连接附加到您的快速应用程序的每个请求:
app.use((req, res, next) => {
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
(error, conn) => {
if (err)
{
console.log(err);
}
else
{
req._oracledb = conn;
next();
}
});
现在,当用户发出请求时,您可以使用如下连接:
app.get('/', (req,res) => { req._oracledb.query('queryString', callback });
您的版本将是:
app.get('/',function(req,res){
req._oracledb.query('SELECT * from employees', function(err, rows, fields)
{
connection.end();
if (!err)
{
console.log('The solution is: ', rows);
}
else
{
console.log('Error while performing Query.');
}
});
});
在通过getConnection建立连接后,没有必要使用Connection.connect,因为连接已经打开。
希望这有帮助
答案 2 :(得分:0)
我按照建议重新排列代码。但仍然出现错误
var express = require('express');
var oracledb = require('oracledb');
var app = express();
var dbConfig = require('./dbconfig.js');
// Get a non-pooled connection
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.execute(
// The statement to execute
app.get("/",function(req,res){
connection.query('SELECT * from employees', function(err, rows, fields) {
connection.end();
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
// The callback function handles the SQL execution results
function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
doRelease(connection);
});
});
// Note: connections should always be released when not needed
function doRelease(connection) {
connection.close(
function(err) {
if (err) {
console.error(err.message);
}
});
}
app.listen(3000);