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

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Apr 18, 2013 at 21:31 yas4891yas4891 4,8623 gold badges36 silver badges56 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.', 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