0

我尝试使用 node js、express 和 OracleDB 制作 API。我在 server.js 中的示例代码如下。在命令行界面中创建 api 时出现以下错误,即 Connection.connect 不是函数。请帮我解决这个问题。

服务器.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"
}
4

3 回答 3

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(....”行似乎没有必要。

于 2018-06-08T09:36:27.517 回答
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);
于 2018-06-08T10:53:09.140 回答
0

这是因为 getConnection 不返回连接它返回一个承诺。您可以设置 express 中间件以将连接附加到您的 express 应用程序的每个请求:

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.');
    }
  });
});

Connection.connect 在您通过 getConnection 获得连接后不再需要,因为连接已经打开。

希望这可以帮助

于 2018-06-08T09:38:25.787 回答