简体   繁体   English

NodeJS回调

[英]NodeJS Callbacks

I've spent a few hours playing with my callback but it refuses to work! 我已经花了几个小时来处理我的回调,但是它拒绝工作! 'trying' outputs to the console fine, but it always refuses to output 'callback here'.... Am I missing something? “尝试”输出到控制台很好,但是它始终拒绝输出“此处回调”。...我是否缺少某些内容?

I previously has var output = job.runJob(.... which worked fine, but I needed to add in some async jazz for job's to run! 我以前有var output = job.runJob(....的效果很好,但是我需要添加一些异步爵士乐才能运行job!

jobscheduler.js 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 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 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');
    }
};

You are passing four arguments to runjob, but runjob only takes 3 arguments. 您将四个参数传递给runjob,但是runjob仅需要3个参数。 Change the definition of runjob like so: 像这样更改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);
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM