Javascript oracledb中未定义连接

Javascript oracledb中未定义连接,javascript,node.js,oracle,es6-promise,node-oracledb,Javascript,Node.js,Oracle,Es6 Promise,Node 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: Unh

我正在使用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);
            }
        }
    }

您可以尝试将连接添加到在
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中的作用域,它可能会帮助您解决未来的问题。这里有一个链接:

如果您可以使用
wait
,那么您就使用了
异步
功能。如果您使用的是
async
函数,为什么要使用承诺链

下面是这种类型的代码与承诺的关系:

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;
有关更多信息,请参阅本系列:

它应该是连接,而不是连接。