【问题标题】:Create API with node js, express, and OracleDB使用 node js、express 和 OracleDB 创建 API
【发布时间】:2020-10-15 07:21:02
【问题描述】:

我尝试使用 node 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"
}

【问题讨论】:

    标签: javascript node.js express node-oracledb


    【解决方案1】:

    你应该像这样使用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(...." 行似乎没有必要。

    【讨论】:

      【解决方案2】:

      这是因为 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.');
          }
        });
      });
      

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

      希望对你有帮助

      【讨论】:

      • 嗨,Bird,我按照你的建议重新排列了代码,但出现了一个错误 TypeError: req._oracledb.query is not a function
      • @Karthick 你有没有把中间件放在 .get 之前?你能链接一个pastebin吗?
      【解决方案3】:

      我按照建议重新排列代码。但仍然出现错误

      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);
      

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 2020-12-22
        • 2018-05-13
        • 2021-02-25
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 2020-12-19
        • 2021-12-21
        相关资源
        最近更新 更多