奇怪的 node.js 错误:TypeError: Object #没有“on”方法;

发布于 2024-10-20 08:04:58 字数 2534 浏览 4 评论 0原文

我正在尝试在 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));

    });
}

UPDATE

现在我注意到在第一个请求中,我得到了这个:

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)

I'm trying to run a simple screen scraping app in node.js. The code is posted here:
https://github.com/anismiles/jsdom-based-screen-scraper
http://anismiles.wordpress.com/2010/11/29/node-js-and-jquery-to-scrape-websites/

The server starts up fine, but then when I run a query on it, I get the following error. Does anyone know why that is?

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)

The function that appears to be throwing the error is:

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

    });
}

UPDATE

Now I noticed that on the first request, I get this:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2024-10-27 08:04:58

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

The only explanation that comes to mind is that the required module is not an EventEmmiter instance. Therefore, it does not have an 'on' method.

ゝ杯具 2024-10-27 08:04:58

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

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

  2. searcher-rediff.jssearcher-flipkart.jssearcher-landmarkonthenet.js 扩展自 searcher.js< /code>,因此它们也继承自 EventEmitter


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

因此,我认为,由于某种原因,searcher.js 无法从 EventEmitter 继承,因此缺少“on”方法。

Fundamental reason behind this bug should be something to do with the EventEmittter. Allow me to explain,

  1. searcher.js inherits from EventEmitter
    (Line-26) Searcher.prototype = new process.EventEmitter;

  2. searcher-rediff.js, searcher-flipkart.js and searcher-landmarkonthenet.js extend from searcher.js, so they also inherit from EventEmitter.

  3. ‘on’ method is actually defined in EventEmitter.

So, I think, for some reason, searcher.js is not able to inherit from EventEmitter and hence the method ‘on’ is missing.

傲娇萝莉攻 2024-10-27 08:04:58

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

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

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

最后你可能会得到类似的结果......

 var rediff = require('./searcher-rediff').searcher;

You are trying to setup a listener on a module instead of an EventEmitter.

require('./searcher-rediff'); returns a Javascript module. I imagine there is actually an object in the searcher-rediff module that is the EventEmitter you are looking for.

Look through the searcher-rediff.js code to see where an EventEmitter is defined and exported, then you'll need to reference that.

In the end you'll probably end up with something like...

 var rediff = require('./searcher-rediff').searcher;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文