javascript - module.exports.report.logon 不是一个函数

标签 javascript node.js cognos

我试图让登录在调度程序上工作,但出现 module.exports.report.logon is not a function 错误。这是代码:

const cron = require('node-cron');
const cms = require("g4js-cognos").Cms;
module.exports.report = new cms(url, namespace, usr, psw);

let task = cron.schedule('*/1 * * * *', function() {
    logonToCognos();
}, true);

task.start();

function logonToCognos() {
    module.exports.report.logon().then((response) => {  // THE ERROR
        console.log(" LOGGED on, status: " + response.statusCode);
    }).catch((error) => {
        console.log(" Logon on fail: " + error);
    });
}

当我不使用调度程序并且 module.exports.report 位于函数之外时,一切正常:

const cms = require("g4js-cognos").Cms;
module.exports.report = new cms(url, namespace, usr, psw);

module.exports.report.logon().then((response) => {
    console.log(" LOGGED on, status: " + response.statusCode);
}).catch((error) => {
    console.log(" Logon on fail: " + error);
});

另外,如果我不使用 module.exports,它工作正常(但我必须使用导出,因为我在另一个模块中需要它):

const cms = require("g4js-cognos").Cms;
const report = new cms(url, namespace, usr, psw);

let task = cron.schedule('*/1 * * * *', function() {
    logonToCognos();
}, true);

task.start();

function logonToCognos() {
    report.logon().then((response) => {  // NO ERROR
        console.log(" LOGGED on, status: " + response.statusCode);
    }).catch((error) => {
        console.log(" Logon on fail: " + error);
    });
}

有什么想法吗?为什么 module.exports 的工作方式如此不同?谢谢。

最佳答案

试试这个:

const cron = require('node-cron');
const cms = require("g4js-cognos").Cms;

// init your class
const report = new cms(url, namespace, usr, psw);

// define what to do after logon
report.logon().then((response) => { 
  console.log(" LOGGED on, status: " + response.statusCode);
}).catch((error) => {
  console.log(" Logon on fail: " + error);
});

// define cron job
const task = cron.schedule('*/1 * * * *', function() {
  report.logon();
}, true);

// start cron job
task.start();

// finally export your class
module.exports.report = report;

也许这可以帮助你module.exports

关于javascript - module.exports.report.logon 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40845499/

相关文章:

javascript - 按钮需要点击两次才能触发功能

javascript - Bunyan with webpack - 尝试访问已被填充的 fs

javascript - 通过 R 和子进程在错误的目录中创建文件

cognos - 我可以向 Cognos 列表报告添加空行吗?

javascript - JavaScript 正则表达式中的 ASCII 7 响铃字符

javascript - 如何将网页向下推以适合顶部栏

node.js - 如何在 adonis5 中创建路径和查询验证

javascript - 使用 node.js 读取 express.js 中的参数的正确语法是什么

报告工作室模板

java - 如何使用登录名/密码连接到 Java 中的 IBM Cognos BI?