admin管理员组文章数量:1323342
According to this question here I need to call request.setMaxListeners(0)
to solve the problem described.
When I try to do:
var request = require('request');
request.setMaxListeners(0);
I get the following error message:
request.setMaxListeners(0);
^ TypeError: Object function request(uri, options, callback) { if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.') if ((typeof options === 'function') && !callback) callback = options if (options && typeof options === 'object') {
options.uri = uri } else if (typeof uri === 'string') {
options = {uri:uri} } else {
options = uri }
options = copy(options)
if (callback) options.callback = callback var r = new Request(options) return r } has no method 'setMaxListeners'
at Object.<anonymous> (/home/vagrant/twitter/test.js:3:9)
at Module._pile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
How do I correctly adjust the default value for setMaxListeners
when using the request-module?
node --version: 0.10.4
request module version: 2.16.6
According to this question here I need to call request.setMaxListeners(0)
to solve the problem described.
When I try to do:
var request = require('request');
request.setMaxListeners(0);
I get the following error message:
request.setMaxListeners(0);
^ TypeError: Object function request(uri, options, callback) { if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.') if ((typeof options === 'function') && !callback) callback = options if (options && typeof options === 'object') {
options.uri = uri } else if (typeof uri === 'string') {
options = {uri:uri} } else {
options = uri }
options = copy(options)
if (callback) options.callback = callback var r = new Request(options) return r } has no method 'setMaxListeners'
at Object.<anonymous> (/home/vagrant/twitter/test.js:3:9)
at Module._pile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
How do I correctly adjust the default value for setMaxListeners
when using the request-module?
node --version: 0.10.4
request module version: 2.16.6
1 Answer
Reset to default 8EDIT
After a bit disscussion i think you are trying to do something like this, after "setMaxListeners"
var request = require('request');
// this will fail
request.setMaxListeners(0);
request('http://www.google.', function (error, response, body) {
// do something ...
})
Step by step
var request = require('request');
The request module is required. This module exports the function request.
This function does not inherit from the EventEmitter - an internal node "class" - so it does not have the "setMaxListeners method".
With this in mind, the following line will fail
request.setMaxListeners(0);
You need a Request object, that inherits from the EventEmitter and has the desired method.
To get one, simply call the request function. On the value returned by this function, you can call "setMaxListeners".
var request_object = request('http://www.google.')
request_object.setMaxListeners(0)
Better if we chain the calls
request('http://www.google.').setMaxListeners(0)
Warning
I do not reend at all removing the max listeners limit. Some kind of infinite loop may be the problem - maybe a listener being bound on each "request" event - to throw a "too much listeners bound" error. This error is an effective method to detect memory leaks, so i remend raising a bit the "maxListeners" limit rather than setting it to "unlimited".
To do this, pass a int != 0. As a note, the default value is 10.
request(
//
// request stuff goes here
//
).setMaxListeners(20)
The Answer
var request = require('request');
request('http://www.google.', function (error, response, body) {
//
// do something ...
//
}).setMaxListeners(20)
本文标签: javascriptHas no method 39setMaxListeners39 errorStack Overflow
版权声明:本文标题:javascript - Has no method 'setMaxListeners' error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742134314a2422302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论