admin管理员组文章数量:1302428
I'm trying to run a simple screen scraping app in node.js. The code is posted here: /
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.onIning (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('pleted', 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._pile (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._pile (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./anismiles/jsdom-based-screen-scraper http://anismiles.wordpress./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.onIning (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('pleted', 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._pile (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._pile (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)
Share
Improve this question
edited May 25, 2023 at 16:08
aynber
23k9 gold badges54 silver badges67 bronze badges
asked Mar 3, 2011 at 20:46
AvishaiAvishai
4,7605 gold badges45 silver badges67 bronze badges
3 Answers
Reset to default 3The only explanation that es to mind is that the required module is not an EventEmmiter instance. Therefore, it does not have an 'on' method.
Fundamental reason behind this bug should be something to do with the EventEmittter
. Allow me to explain,
searcher.js
inherits fromEventEmitter
(Line-26)Searcher.prototype = new process.EventEmitter
;searcher-rediff.js
,searcher-flipkart.js
andsearcher-landmarkonthenet.js
extend fromsearcher.js
, so they also inherit fromEventEmitter
.‘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.
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;
本文标签: javascriptStrange nodejs error TypeError Object ltObjectgt has no method 39on39Stack Overflow
版权声明:本文标题:javascript - Strange node.js error: TypeError: Object #<Object> has no method 'on' - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741652137a2390540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论