Skip to content
Advertisement

JavaScript Callback method not able to call function imported from module

I am trying to implement Oracle database change notification in NodeJS.

Function subscribeTimesheetEvent is subscribing to the notification, and one of the inputs is the callback method. In my case it’s myCallback function. This function gets called and it works fine, except it doesn’t see executeQuery function imported from dbFunctions file. I have used this function at other place by importing from dbFunctions and it works fine. I suspect I am facing some scoping issues I am not well versed with, in the myCallback function.

const oracledb = require('oracledb');
const logger = require('../logger')
const common = require('../common');
let { executeQuery } = require('../db/dbFunctions');
const { getQueryObj, queries } = require('../db/queries')

require('dotenv').config();


function myCallback(message) {
            logger.log(logger.LOG_LEVEL.INFO, JSON.stringify(message));
            logger.log(logger.LOG_LEVEL.INFO, message.type);
            if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) {
                // clearInterval(interval);
                logger.log(logger.LOG_LEVEL.INFO, "Deregistration has taken place...");
                return;
            }
            message.tables.forEach(table => {
                logger.log(logger.LOG_LEVEL.INFO, `--> --> Table Name: ${table.name}`);
                // Note table.operation and row.operation are masks of
                // oracledb.CQN_OPCODE_* values
                logger.log(logger.LOG_LEVEL.INFO, `--> --> Table Operation: ${table.operation}`);
                if (table.rows) {
                    logger.log(logger.LOG_LEVEL.INFO, `--> --> Table Rows: table.rows.length`);
                    table.rows.forEach(row => {
                        if(row.operation ==oracledb.CQN_OPCODE_INSERT ){
                            executeQuery("select * from chatbot_msg where rowid = :rowid", [row.rowid])
                                .then(msg=>logger.log(logger.LOG_LEVEL.INFO, JSON.stringify(msg)));
                            
                        }
                    });
                }
            });
            logger.log(logger.LOG_LEVEL.INFO, Array(61).join("="));
            // }  
        }
    
async function subscribeTimesheetEvent() {
    logger.log(logger.LOG_LEVEL.INFO, 'Registering Oracle Change Notification');
    const connection = await oracledb.getConnection();
    // await connection.unsubscribe('mysub');

    const options = {
        sql: `SELECT * FROM chatbot_msg`,  // query of interest
        callback: myCallback,                // method called by notifications
        qos: oracledb.SUBSCR_QOS_ROWIDS,
        port: 9091,
        timeout: 120,
        operations: oracledb.CQN_OPCODE_INSERT
    };

    await connection.subscribe('tsMsgSub', options);
}

module.exports.subscribeTimesheetEvent = subscribeTimesheetEvent;

Debug Screenshot: executeQuery shows up as undefined.

enter image description here

Advertisement

Answer

Importing the whole file instead of destructuring solves the error. Not sure why though ..

Import Code:

const dbFunctions = require('../db/dbFunctions');

Call the function:

dbFunctions.executeQuery("select * from chatbot_msg where rowid = :rowid", [row.rowid])
                                .then(msg=>logger.log(logger.LOG_LEVEL.INFO, JSON.stringify(msg)));
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement