【问题标题】:NodeJS CallbacksNodeJS 回调
【发布时间】:2014-01-31 21:58:01
【问题描述】:

我花了几个小时玩我的回调,但它拒绝工作! 'trying' 输出到控制台很好,但它总是拒绝输出'callback here'....我错过了什么吗?

我以前有 var output = job.runJob(.... 工作正常,但我需要添加一些异步爵士乐才能运行作业!

jobscheduler.js

job = require("./job"),

console.log('trying');
job.runJob(item.definition,item.vars,item._id, function(callback) {
    console.log('['+item._id+'] Job output = '+callback);
    console.log('callback here');

    // Update job nextrun time
    var nextrun = new Date();
    nextrun.setSeconds(nextrun.getSeconds() + 10);

    collection.update({'_id':item._id}, {$set: {nextrun:nextrun}}, {safe:true}, function(err, result) {
        if (err) {
                console.log('Error updating item: ' + err);
            } else {
                console.log('['+item._id+'] Job was updated. Next run is '+nextrun);
            }
        });
});

job.js

var jobdef = require("./jobdef");

module.exports = {
    runJob : function(job,vars,jobid){
        if (typeof jobdef[job] == 'function') { 
            var output = jobdef[job](vars,jobid);
            return output;
        } else {
            console.log('['+jobid+'] Job def \'%s\' does not exist. Check jobdef.js for definitions.',job);
        }
    }
};

jofdef.js

module.exports = {
    ping : function(vars,jobid){
        return('Were in ping');
    },

    urlopen : function(vars,jobid){
        return('Were in urlopen');
    },

    findstr : function(vars,jobid){
        return('Were in findstr');
    }
};

【问题讨论】:

    标签: javascript node.js callback


    【解决方案1】:

    您将四个参数传递给 runjob,但 runjob 只需要 3 个参数。像这样改变runjob的定义:

    module.exports = {
        runJob : function(job, vars, jobid, callback){
            if (typeof jobdef[job] == 'function') { 
                var output = jobdef[job](vars,jobid);
                if (typeof callback === 'function')
                    callback(output);
            } else {
                console.log('['+jobid+'] Job def \'%s\' does not exist. Check jobdef.js for definitions.',job);
            }
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 2012-09-08
      • 2018-09-26
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多