node-js-libs / node.io

MIT License
1.8k stars 140 forks source link

Output phase being skipped or am I doing something wrong? #117

Closed marcoroest closed 12 years ago

marcoroest commented 12 years ago

Hello,

I'm pretty new to node.js and node.io (I've been looking at it for a day or 2 now), so please bear with me.

It seems that when I'm running my script the output phase is skipped. I'am able to console.log() to result when the job is done though.

This is my code:

var EventEmitter = require('events').EventEmitter
    , on = EventEmitter.prototype.on;
EventEmitter.prototype.on = function () {
    this._maxListeners = Infinity;
    on.apply(this, arguments);
};

var nodeio = require('node.io');
var jobOptions = { timeout : 99, max: 100 };
var job = {
    input: 'test.txt',
    run: function (someNumber) {
        this.getHtml("http://www.some.url/?parameter=" + someNumber, function (err, $) {
            // exit when erroring
            if (err) {
                this.skip();
            }
            try {
                var result = [];
                result = getData($, someNumber, result);
                this.emit(result);
            } catch(err) {
                this.skip();
            }
        });
    },
    output: 'output.txt'
};

function getData($, someNumber, result) {
    $('.someClass tr').each(function (tr) {
        result.push(someNumber + ', ' + tr.fulltext);
    });
    return result;
}

nodeio.start(new nodeio.Job(jobOptions, job), function(err, result) {
    console.log(result);
}, true);

I've also tried to use a function as output, this way, but still nothing.

    output: function () {
        console.log('this text is not showing');
    }

I'm running the latest version of node.js and node.io

Any help would be appreciated greatly.

chriso commented 12 years ago

The third parameter to nodeio.start() is either true to capture output in the callback or false (default) to use the output method you've defined.

Try this

nodeio.start(new nodeio.Job(jobOptions, job), function(err) {
    if (err) throw err;
});
marcoroest commented 12 years ago

Cool i'll give that a try. Thanks for the quick response and this awesome project :)