【问题标题】:Handling error in CronJob executing function处理 CronJob 执行函数中的错误
【发布时间】:2015-09-06 17:56:07
【问题描述】:

我正在使用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. */
);

【问题讨论】:

  • 视情况而定。什么样的错误?
  • 以查询数据库时出错为例

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


【解决方案1】:

只需输入 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. */
);

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2021-10-09
    • 2021-12-04
    • 2017-12-06
    • 2013-09-29
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多