在 NodeJS 中将数据插入 oracle 数据库 table

Insert data into an oracle database table in NodeJS

我想使用以下代码中引用的 insertIntoTable 将数据插入 Oracle 数据库 table;该函数不工作,但没有它,查询按预期工作。

let connection;
var oracledb = require('oracledb');

(async function() {
try{

    connection = await oracledb.getConnection({
    user : 'demo7',
    password : 'dbpass',
    connectString : 'localhost/induspdb'
    });
    console.log("Successfully connected to Oracle!");

  //function which insert result into table
    async function insertIntoTable(dateToday, fileFound, fileNotFound )
    {
    const query='insert into backupinfo (infdate,found,notfound) values (:1,:2,:3)';
    var binds=[dateToday,fileFound,fileNotFound];
    await connection.execute(query , binds, {autoCommit:true}); 
    }

  // module.exports.insertIntoTable=insertIntoTable;
  insertIntoTable('2019-09-06','rtx','agh');

} catch(err) {
    console.log("Error: ", err);
  } finally {
    if (connection) {
      try {
        await connection.close();
      } catch(err) {
        console.log("Error when closing the database connection: ", err);
      }
    }
  }

})()

每当我调用此函数并传递给定参数时,此函数应将值插入 table,这是错误输出:

Successfully connected to Oracle!

    (node:10088) UnhandledPromiseRejectionWarning: Error: DPI-1010: not connected
    (node:10088) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an as
    ync function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
    (node:10088) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are n
    ot handled will terminate the Node.js process with a non-zero exit code.

根据数据库中的 table 类型检查绑定的所有参数是否有效。 另外,在insertIntoTable之前加上"await"这样报错就不会是UnhandledPromiseRejection了,会更清楚

在完成连接之前不要释放连接,否则您将收到 DPI-1010 错误。再次检查您的异步逻辑