简体   繁体   English

没有方法'setMaxListeners'错误

[英]Has no method 'setMaxListeners' error

According to this question here I need to call request.setMaxListeners(0) to solve the problem described. 根据这个问题,我需要调用request.setMaxListeners(0)来解决所描述的问题。

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._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

How do I correctly adjust the default value for setMaxListeners when using the request-module? 使用请求模块时,如何正确调整setMaxListeners的默认值?

node --version: 0.10.4 node --version:0.10.4
request module version: 2.16.6 请求模块版本:2.16.6

EDIT 编辑

After a bit disscussion i think you are trying to do something like this, after "setMaxListeners" 经过一番讨论之后,我认为你在“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 . 该模块导出功能请求

This function does not inherit from the EventEmitter - an internal node "class" - so it does not have the "setMaxListeners method" . 此函数EventEmitter继承 - 一个内部节点“class” - 因此它没有“setMaxListeners方法”

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. 您需要一个Request对象 ,它继承自EventEmitter并具有所需的方法。

To get one, simply call the request function. 要获得一个,只需调用请求函数。 On the value returned by this function, you can call "setMaxListeners". 在此函数返回的值上,您可以调用“setMaxListeners”。

var request_object = request('http://www.google.com')
request_object.setMaxListeners(0)

Better if we chain the calls 如果我们链接电话会更好

request('http://www.google.com').setMaxListeners(0)

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" . 此错误是检测内存泄漏的有效方法,因此我建议提高一点“maxListeners”限制,而不是将其设置为“无限制”

To do this, pass a int != 0. As a note, the default value is 10. 为此,传递一个int!= 0.注意,默认值为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM