簡體   English   中英

NodeJS回調

[英]NodeJS Callbacks

我已經花了幾個小時來處理我的回調,但是它拒絕工作! “嘗試”輸出到控制台很好,但是它始終拒絕輸出“此處回調”。...我是否缺少某些內容?

我以前有var output = job.runJob(....的效果很好,但是我需要添加一些異步爵士樂才能運行job!

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

您將四個參數傳遞給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);
        }
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM