Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: undefined is not a function #741

Open
mpjura opened this issue Sep 13, 2015 · 2 comments
Open

Uncaught TypeError: undefined is not a function #741

mpjura opened this issue Sep 13, 2015 · 2 comments

Comments

@mpjura
Copy link

mpjura commented Sep 13, 2015

Looking through open and closed issues, it seems a lot of folks are having problems similar to this. Various methods fail inconsistently with Uncaught TypeError: undefined is not a function. Mostly PUT.

Code to reproduce is here. Failures are inconsistent. I can get it to fail roughly 20% of the time.

var expect = require( "chai" ).expect;

describe( "SuperAgent", function() {
    var url = "http://test/foo",

    request = require( "superagent" ),
    server = require( "superagent-mocker" )( request );

    before( function() {
        server.get( url, function() {
            return "get all";
        });

        server.post( url, function() {
            return "post";
        });

        server.get( url + "/:id", function( req ) {
            return "get " + req.params.id;
        });

        server.put( url + "/:id", function( req ) {
            return "put " + req.params.id;
        });

        server.del( url + "/:id", function( req ) {
            return "del " + req.params.id;
        });
    });

    it( "GET all fails inconsistently", function( done ) {
        request.get( url ).end( function( err, res ) {
            expect( res ).to.equal( "get all" );
            done();
        });
    });

    it( "POST fails inconsistently", function( done ) {
        request.post( url )
        .set( { "Content-Type": "application/json" } )
        .send( { foo: "bar" } )
        .end( function( err, res ) {
            expect( res ).to.equal( "post" );
            done();
        });
    });

    it( "GET fails inconsistently", function( done ) {
        request.get( url + "/1" ).end( function( err, res ) {
            expect( res ).to.equal( "get 1" );
            done();
        });
    });

    it( "PUT fails inconsistently", function( done ) {
        request.put( url + "/1" )
        .set( { "Content-Type": "application/json" } )
        .send( { foo: "bar" } )
        .end( function( err, res ) {
            expect( res ).to.equal( "put 1" );
            done();
        });
    });

    it( "DELETE fails inconsistently", function( done ){ 
        request.del( url + "/1" ).end( function( err, res ) {
            expect( res ).to.equal( "del 1" );
            done();
        });
    });
} );

Mocha reporter output after a failure:

SuperAgent
    ✓ GET all fails inconsistently
    ✓ POST fails inconsistently
    ✓ GET fails inconsistently
    1) PUT fails inconsistently
    ✓ DELETE fails inconsistently


  4 passing (25ms)
  1 failing

  1) SuperAgent PUT fails inconsistently:
     Uncaught TypeError: undefined is not a function
      at Socket.socketErrorListener (_http_client.js:271:9)
      at net.js:950:16
@mpjura
Copy link
Author

mpjura commented Sep 13, 2015

Similar issue: #687

@dafortune
Copy link

Hey Guys, I am having a similar issue, not only with "PUT" request but also with "GET" requests, I traced it back up to https://github.com/visionmedia/superagent/blob/master/lib/node/index.js#L736. #set and other methods internally calls #request() to create a request to work on. _callback attribute is set on #end, so _callback is not defined at that point. It seems that sometimes node emits an error before #end gets called and so we get this exception.

request.put( url + "/1" )
        // request() is called by `set` which creates the http request object and 
        // attaches `error` event handler, `#_callback` is not defined at this point. 
        // Sometimes node seems to emit an error at this point, before "end", which 
        // seems to be causing the problem.
        .set( { "Content-Type": "application/json" } )
        .send( { foo: "bar" } )
        .end( function( err, res ) {
            expect( res ).to.equal( "put 1" );
            done();
        });

A workaround this issue that worked for me:

    var req = request.put( url );

    // WORKAROUND: https://github.com/visionmedia/superagent/issues/741
    req._callback = function() {};

    req.set(...)

I could create a PR to fix this if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants