How to solve Null response error in Node Scheduler?

48 views Asked by At

I am using the node scheduler to schedule my jobs, but getting Null in return, and nothing is printed when I try with try and catch the block, but still job returns null.

  const jobDate = new Date();
  const jobTz = 'Indian/Reunion';

  const job = schedule.scheduleJob({ date: jobDate, tz: jobTz }, function () {
    console.log('Job Running');
  }); 

If I do console.log(job) then get null. Not sure what the error is and why this is happening.

1

There are 1 answers

0
Ankit Kumar On

I found a solution for that, this issue occurs when we pass { date: jobDate, tz: jobTz }. To solve this error we can use moment-timezone package.

const startDateTime = moment.tz(jobDate, jobTz).subtract(1, "hour").toDate();

const job = schedule.scheduleJob(startDateTime, function () {
    console.log('Job Running');
});

If we want to use timezone then this is best way to handle. Else we can directly pass startDateTime in scheduler function.

In response we can get like job.name to get unique job Id.