Search code examples
javascriptnode.jsnode-oracledb

Insert data into an oracle database table in NodeJS


I would like to insert data into an Oracle database table by using insertIntoTable which is referenced in the code below; The function isn't working but without it, the query works as intended.

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

})()

Whenever I call this function and pass given arguments, this function should insert values into the table, here is the error output:

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.

Solution

  • Check that all the params of the binds are valid according the table type in the db. In addition, add before insertIntoTable "await" so the error wont be UnhandledPromiseRejection, and will be clearer.