TooTallNate / superagent-proxy

`Request#proxy(uri)` superagent extension

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot read property 'get' of undefined

herlon214 opened this issue · comments

My code:

var request = require( 'superagent' ).agent();
var version = require( '../../package' ).version;
// extend with Request#proxy()
require('superagent-proxy')(request);

// HTTP, HTTPS, or SOCKS proxy to use
var proxy = process.env.http_proxy || 'http://189.126.49.98:8080';

module.exports = function( options ){
  if( !options ) options = {};
  if( !options.store ) options.store = ( require( '../storage/null' ) )();
  return function( uri, cb ){
    var hit = options.store.get( uri );
    if( hit ) return cb.apply( null, hit );
    request.get( uri )
           .proxy(proxy)
           .set( 'User-Agent', "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0" )
           .end( function( err, res ) {
      if( err ) return cb( err, { uri: uri } );
      var result = [ err, {
        uri: uri,
        statusCode: res.status,
        headers: res.header,
        body: res.text || ''
      } ];
      options.store.set( uri, result );
      return cb.apply( null, result );
    });
  };
};

Can you show me a full stack trace?

I've got the same problem.

Running 1.0.0 tag.

$ npm list | grep super
├─┬ superagent@1.2.0
├─┬ superagent-proxy@1.0.0 (git+https://github.com/TooTallNate/superagent-proxy/#f81350e0c6a7e88b872a4d00ebfeb6fe9073bb61)
├─┬ supertest@1.0.1
│ └─┬ superagent@1.2.0
/home/jotham/x/x/modules/notifications/node_modules/superagent-proxy/index.js:92
  if (desc.get == getUrl && desc.set == setUrl) return; // already patched
          ^
TypeError: Cannot read property 'get' of undefined
    at setupUrl (/home/jotham/x/x/modules/notifications/node_modules/superagent-proxy/index.js:92:11)
    at Function.proxy (/home/jotham/x/x/modules/notifications/node_modules/superagent-proxy/index.js:68:3)
    at setup (/home/jotham/x/x/modules/notifications/node_modules/superagent-proxy/index.js:50:18)
    at Object.<anonymous> (/home/jotham/x/x/modules/notifications/test/routes.js:7:28)
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at /usr/local/lib/node_modules/mocha/lib/mocha.js:192:27
    at Array.forEach (native)
    at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:189:14)
    at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:422:31)
    at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:398:16)
    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:906:3

@herlon214 It's not working for you since you're attempting to patch a superagent.Agent instance, not the superagent module itself. Change:

// extend with Request#proxy()
require('superagent-proxy')(request);

to:

// extend with Request#proxy()
require('superagent-proxy')(require('superagent'));

Let's create a new issue for any further troubles /cc @dougludlow @jotham