问题描述
根据,我需要致电 request.setMaxListeners(0)
解决所描述的问题。
According to this question here I need to call request.setMaxListeners(0)
to solve the problem described.
当我尝试这样做时:
var request = require('request');
request.setMaxListeners(0);
我收到以下错误消息:
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._compile (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
在使用请求模块时,如何正确调整 setMaxListeners
的默认值?
How do I correctly adjust the default value for setMaxListeners
when using the request-module?
node --version:0.10.4
请求模块版本:2.16.6
推荐答案
编辑
经过一番讨论后,我想你正在尝试做点什么像这样,在setMaxListeners之后
EDIT
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.com', function (error, response, body) {
// do something ...
})
一步一步
Step by step
var request = require('request');
请求模块是必需的。此模块导出。
The request module is required. This module exports the function request.
此功能不继承自
- 内部节点class - 因此它没有。
考虑到这一点,以下行将失败
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.
要获得一个,只需调用请求函数即可。在此函数返回的值上,您可以调用setMaxListeners。
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.com')
request_object.setMaxListeners(0)
如果我们将来电
request('http://www.google.com').setMaxListeners(0)
警告
我根本不推荐删除最大侦听器限制。某种无限循环可能是问题 - 可能是每个请求事件都绑定一个侦听器 - 抛出太多侦听器绑定错误。 此错误是检测内存泄漏的有效方法,因此我建议提高一点maxListeners限制,而不是将其设置为无限制。
Warning
I do not recomend 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 recommend raising a bit the "maxListeners" limit rather than setting it to "unlimited".
要执行此操作,请传递一个int!= 0.注意,默认值为10.
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.com', function (error, response, body) {
//
// do something ...
//
}).setMaxListeners(20)
这篇关于没有方法'setMaxListeners'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!