I created a file db.js that connect to the Oracle database.

process.env.ORA_SDTZ = 'UTC';
var oracledb = require('oracledb');
exports.saveInDB = async function(data) {
  let connection;

  try {
    let sql, binds, options, result;
    binds = [];

    connection = await oracledb.getConnection({
      user: "user",
      password: "pass",
      connectString: "localhost/dbname"
    });

    var start = new Date();

    for (var i = 1; i <= 100; i++) {
      sql = `INSERT INTO mytab VALUES (:1, :2, :3)`;
      binds = [
        [i, "Inserted row number => " + i + " =>" + data, 0]
      ];
      options = {
        autoCommit: true,
        bindDefs: [
          { type: oracledb.NUMBER },
          { type: oracledb.STRING, maxSize: 999 },
          { type: oracledb.NUMBER }
        ]
      };
      result = await connection.executeMany(sql, binds, options);

      console.log("Inserted row number => ", i);
    }

    var end = new Date() - start;
    console.info('Execution time: %dms', end);
  } catch (err) {
    console.error(err);
  } finally {
    if (connection) {
      try {
        await connection.close();
      } catch (err) {
        console.error(err);
      }
    }
  }
}

When I want to call var db = require('./db.js'); db.saveInDb(data);, This error occurred : TypeError: db.saveInDb is not a function. I try the other format to define the method like async function saveInDb(data) but the result not changes. How can I fix this error? I search for this but can not fix this. Thanks.

3 Answers

0
Shivani Tyagi On

Try with:

export const saveInDB = async function(data) {
            let connection;
            try {
    // my codes....
                }
                var end = new Date() - start;
                console.info('Execution time: %dms', end);
            } catch (err) {
                console.error(err);
            } finally {
                if (connection) {
                    try {
                        await connection.close();
                    } catch (err) {
                        console.error(err);
                    }
                }
            }
        }
0
danny.hu On

    process.env.ORA_SDTZ = 'UTC';
    var oracledb = require('oracledb');
    const saveInDB = async function(data) {
        let connection;
        try {
// my codes....
            }
            var end = new Date() - start;
            console.info('Execution time: %dms', end);
        } catch (err) {
            console.error(err);
        } finally {
            if (connection) {
                try {
                    await connection.close();
                } catch (err) {
                    console.error(err);
                }
            }
        }
    }
    
    export { saveInDB }

0
DEVCNN On

The problem with your approach is that async function(){ returns a Promise which you are trying to call as a function in var db = require('./db.js'); db.saveInDb(data);, Hence the error. Execute the promise appropriately(depends what's in the db.js file) and this can be resolved.