在oracledb

我正在使用oracledb cen node.js模块,并且在进行数据库连接进行选择时,它返回数据,但是还会出现此错误:

(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);
            }
        }
    }
budianxyl 回答:在oracledb

如果可以使用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`,{
          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/

,

您可以尝试将连接添加到在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范围定义的信息,它可能会帮助您解决以后的问题。这是链接:https://www.w3schools.com/js/js_scope.asp

本文链接:https://www.f2er.com/2928574.html

大家都在问