处理CronJob执行函数中的错误

时间:2015-06-21 01:08:19

标签: node.js error-handling callback cron-task

我正在使用CronJob。如果在执行 myFunction

时出错,该怎么办?
var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function myFunction() {
     //Do something
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);

1 个答案:

答案 0 :(得分:0)

只需输入if / else语句即可。这样你的应用程序就不会崩溃。您可以向应用程序添加记录器并记录cron作业中发生的所有错误。

var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function myFunction() {
  User.findOne({_id: user_id}, function(err, results) {
    if (err) {
       console.log(err);
    } else {
       //success
       console.log(results);
    }
  })
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);
相关问题