5

I'm new to nodejs and I have written a nodejs program and scheduling it every minute using the node-schedule. but after running for some time and generating a couple of logs at the console, nodejs throws error that this.job.execute is not a function

here is the code I am using:

var nodeSchedule = require('node-schedule');

runJob();

function runJob(){
    console.log("start");
    nodeSchedule.scheduleJob('0 * * * * *',require('./prodModules.js'));

}

the logs I am getting is:

C:\Users\1060641\Downloads\NodeJS HealthReport\Collector>node src\main\nodejs\collector_main.js
start
Connected
Ready
logged in as Super User
nfs_check running...
NFS Check completed
snapchart_check running...
C:\Users\1060641\node_modules\node-schedule\lib\schedule.js:177
    this.job.execute();
             ^

TypeError: this.job.execute is not a function
    at Job.invoke (C:\Users\1060641\node_modules\node-schedule\lib\schedule.js:177:14)
    at null._onTimeout (C:\Users\1060641\node_modules\node-schedule\lib\schedule.js:445:11)
    at Timer.listOnTimeout (timers.js:92:15)

C:\Users\1060641\Downloads\NodeJS HealthReport\Collector>

I don't think there is anything wrong with my prodModules.js since running it standalone its running fine. Scheduling is throwing errors.

Please help.

3
  • If you think it is an error with the library, so check out the issues in github. That's where I'd look first.
    – mugabits
    Mar 31, 2016 at 14:53
  • are you sure require('./prodModules.js') returns what you think it does? and is it what scheduleJob is expecting as a second parameter?
    – Kevin B
    Mar 31, 2016 at 15:00
  • @Kevin-B : you were right. I didnt envelop the prodModules.js in a function, and calling the js file, thats why it was throwing the error that prodModules is not a function. So I wrapped the whole code in prodModules.js in a export.myFunc=function(){} and its working fine now. Thanks Kevin for hinting :)
    – aiman
    Apr 1, 2016 at 10:17

2 Answers 2

3

The node-scheduler callback must be a function. Change your runJob to something like this:

function runJob() {
    console.log("start");
    nodeSchedule.scheduleJob('0 * * * * *', function () { 
        require('./prodModules.js');
    });
}
2

Try re-importing 'node-schedule', I ran your code with a fresh import, and it works as expected.

To down import on windows cmd : > npm install node-schedule

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.