question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'execute' of undefined

See original GitHub issue

Here is my code, simple test to try and get it to console out every minute to make sure the schedule will work. also tried with rule.minute = (next minute of the hour); and rule.minute = 0;

console.log('Inside Delete Processor');
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
//rule.hour = 0;
rule.minute = null;
		
var deleteSchedule = schedule.scheduleJob(rule, deleteExpiredFiles());

function deleteExpiredFiles(){
	console.log(moment().format('MMM DD, Y hh:mm:ss A'), ': Scheduled Tick');
}

Received same error every time.

C:\NodeCode\Processor\node_modules\node-schedule\lib\schedule.js:177
    this.job.execute();
            ^
TypeError: Cannot read property 'execute' of undefined
    at Job.invoke (C:\NodeCode\Processor\node_modules\node-schedule\lib\schedule.js:177:13)
    at Timeout._onTimeout (C:\NodeCode\Processor\node_modules\node-schedule\lib\schedule.js:479:11)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

7reactions
rtodeacommented, Mar 8, 2017

Hi @PsyTae,

You should not invoke the function, but pass the Function object:

var deleteSchedule = schedule.scheduleJob(rule, deleteExpiredFiles); // note the missing `()`

In:

var deleteSchedule = schedule.scheduleJob(rule, deleteExpiredFiles());

you are invoking the function deleteExpiredFiles by deleteExpiredFiles(). deleteExpiredFiles does not have any return statement (i.e. the return value is undefined).

Thus your line is equivalent to:

var deleteSchedule = schedule.scheduleJob(rule, undefined);

which is further equivalent to:

var deleteSchedule = schedule.scheduleJob(rule);

scheduleJob expects at least 2 proper arguments to actually create a Job. Otherwise it returns null.

When writing anonymous functions you are less likely to invoke it by mistake.

var deleteSchedule = schedule.scheduleJob(rule, function (){
	deleteExpiredFiles();
});

vs

var deleteSchedule = schedule.scheduleJob(rule, (function (){
	deleteExpiredFiles();
})());

Please close the issue if this solves your problem.

1reaction
fplatencommented, Feb 21, 2019

Sorry to reply to this closed issue, but how would I pass an argument to the named function?

I’m currently doing schedule.scheduleJob(new Date(), function() { myNamedFunction(myArgument) });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'execute' of undefined Discord Bot js ...
The reasoning for the is when calling command.execute() you are attempting to call a function named 'execute' in the command's module. Since you ......
Read more >
cannot read property 'execute' of undefined” (JavaScript, Node ...
It generally means that at the point of execution, the variable/object holding the property cannot be found by the script. It may be...
Read more >
Getting the same error (Cannot read property 'execute ... - Reddit
The error message means that client.commands.get('ping') is returning undefined, so it doesn't have an execute method to invoke.
Read more >
[Solved]-How do I fix the error "Cannot read property 'execute ...
Coding example for the question How do I fix the error "Cannot read property 'execute' of undefined"?-discord.js.
Read more >
Cannot read property 'execute' of undefined #556 - GitHub
I have one little problem with my code; My code: commands.js module.exports.execute= { name: 'commands', description: "Embed!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found