JavaScript 回调方法无法调用从模块导入的函数
JavaScript Callback method not able to call function imported from module
我正在尝试在 NodeJS 中实现 Oracle 数据库更改通知。
函数subscribeTimesheetEvent正在订阅通知,输入之一是回调方法。在我的例子中,它是 myCallback 函数。这个函数被调用并且工作正常,除了它没有看到从 dbFunctions 文件导入的 executeQuery 函数。我通过从 dbFunctions 导入在其他地方使用了这个函数,它工作正常。我怀疑我在 myCallback 函数中遇到了一些我不太熟悉的范围问题。
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;
调试截图:
executeQuery 显示为未定义。
导入整个文件而不是解构可以解决错误。不知道为什么..
导入代码:
const dbFunctions = require('../db/dbFunctions');
调用函数:
dbFunctions.executeQuery("select * from chatbot_msg where rowid = :rowid", [row.rowid])
.then(msg=>logger.log(logger.LOG_LEVEL.INFO, JSON.stringify(msg)));
我正在尝试在 NodeJS 中实现 Oracle 数据库更改通知。
函数subscribeTimesheetEvent正在订阅通知,输入之一是回调方法。在我的例子中,它是 myCallback 函数。这个函数被调用并且工作正常,除了它没有看到从 dbFunctions 文件导入的 executeQuery 函数。我通过从 dbFunctions 导入在其他地方使用了这个函数,它工作正常。我怀疑我在 myCallback 函数中遇到了一些我不太熟悉的范围问题。
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;
调试截图: executeQuery 显示为未定义。
导入整个文件而不是解构可以解决错误。不知道为什么..
导入代码:
const dbFunctions = require('../db/dbFunctions');
调用函数:
dbFunctions.executeQuery("select * from chatbot_msg where rowid = :rowid", [row.rowid])
.then(msg=>logger.log(logger.LOG_LEVEL.INFO, JSON.stringify(msg)));