0

I'm trying to get the login to work on a scheduler, but getting the module.exports.report.logon is not a function error. This is the code:

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

When I'm not using scheduller and module.exports.report is outside of a function, everything works fine:

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

Also, if I don't use module.exports, it is working fine (but I have to use exports because I need it in another module):

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

Any ideas? Why is module.exports working so different? Thanks.

akrelj
  • 151
  • 4
  • 18
  • Probably has something to do with scope. You're inside the scope of function logonToCognos so module might not be defined. Have you tried passing the constructed cms as a parameter of the function? – kriddy800 Nov 28 '16 at 13:42
  • If, instead of module.exports.report, I put const report, then it is working fine. Why is module.export behaving so different? – akrelj Nov 28 '16 at 13:51
  • I edited the question to have that example as well. – akrelj Nov 28 '16 at 13:57
  • Are you somehow mutating/re-assigning the exported `report` object in some script that is `require()`ing this module? – mscdex Nov 28 '16 at 14:03
  • I don't think so. The only place I use it is in 1 other file, where I have: const appJs = require("../app"); module.exports.readAll = (req) => { return new Promise((resolve, reject) => { appJs.report.getExportById(reportId, params, 'XML') .then((data) => { resolve(data); }).catch((error) => { reject(error); }); }) }; – akrelj Nov 28 '16 at 14:04

2 Answers2

1

try this:

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;

maybe this can help you module.exports

Community
  • 1
  • 1
Victorious
  • 96
  • 4
0

try injecting module.exports.report:

    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(module.exports.report);
    }, true);

    task.start();

    function logonToCognos(report) {
        report.logon().then((response) => {  // THE ERROR
            console.log(" LOGGED on, status: " + response.statusCode);
        }).catch((error) => {
            console.log(" Logon on fail: " + error);
        });
  }
Gurbakhshish Singh
  • 1,034
  • 1
  • 10
  • 25