奇怪的node.js错误:TypeError:Object#<object>没有方法'on'</object>

时间:2011-03-03 20:46:09

标签: javascript node.js screen-scraping connect

我正在尝试在node.js中运行一个简单的屏幕抓取应用程序。代码发布在这里: https://github.com/anismiles/jsdom-based-screen-scraper http://anismiles.wordpress.com/2010/11/29/node-js-and-jquery-to-scrape-websites/

服务器启动正常,但是当我对它运行查询时,我收到以下错误。有谁知道为什么会这样?

TypeError: Object #<Object> has no method 'on'
    at Object.<anonymous> (/Users/avishai/Downloads/anismiles-jsdom-based-screen-scraper-f0c79d3/searcher-server.js:9:10)
    at param (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
    at param (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:157:15)
    at pass (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
    at Object.router [as handle] (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:168:6)
    at next (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/index.js:218:15)
    at Server.handle (/Users/avishai/.node_libraries/.npm/connect/0.5.10/package/lib/connect/index.js:231:3)
    at Server.emit (events.js:45:17)
    at HTTPParser.onIncoming (http.js:1078:12)
    at HTTPParser.onHeadersComplete (http.js:87:31)

似乎抛出错误的函数是:

function books(app){
    app.get('/:query', function(req, res, next) {

        res.writeHead(200, { 'Content-Type': 'text/html' });

        var rediff = require('./searcher-rediff');
        rediff.on('on_book', function(item){
            res.write(item + "<br/>");
        });
        rediff.on('completed', function(){
            res.end();
        });
        rediff.search(escape(req.params.query));

    });
}

更新

现在我注意到,在第一次请求时,我明白了:

SyntaxError: Unexpected strict mode reserved word
    at Module._compile (module.js:369:25)
    at Object..js (module.js:380:10)
    at Module.load (module.js:306:31)
    at Function._load (module.js:272:10)
    at require (module.js:318:19)
    at Object.<anonymous> (/Users/avishai/Downloads/anismiles-jsdom-based-screen-scraper-f0c79d3/searcher.js:2:15)
    at Module._compile (module.js:374:26)
    at Object..js (module.js:380:10)
    at Module.load (module.js:306:31)
    at Function._load (module.js:272:10)

3 个答案:

答案 0 :(得分:2)

我想到的唯一解释是所需的模块不是EventEmmiter实例。因此,它没有'on'方法。

答案 1 :(得分:1)

此错误背后的根本原因应该与EventEmittter有关。让我解释一下,

  1. searcher.js继承自EventEmitter (第26行)Searcher.prototype = new process.EventEmitter;

  2. searcher-rediff.js,searcher-flipkart.js和searcher-landmarkonthenet.js从searcher.js扩展,所以他们也从EventEmitter继承。

  3. 'on'方法实际上是在EventEmitter中定义的。

  4. 所以,我认为,由于某些原因,searcher.js无法从EventEmitter继承,因此缺少方法'on'。

答案 2 :(得分:0)

您正尝试在模块上设置侦听器而不是EventEmitter。

require('./searcher-rediff');返回一个Javascript模块。我想在searcher-rediff模块中实际上有一个对象是你正在寻找的EventEmitter。

查看searcher-rediff.js代码以查看EventEmitter的定义和导出位置,然后您需要引用它。

最后你可能会得到类似......

之类的东西
 var rediff = require('./searcher-rediff').searcher;