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

Cannot read property 'cookie' of undefined #3

Closed
Stofkn opened this issue Apr 24, 2014 · 5 comments
Closed

Cannot read property 'cookie' of undefined #3

Stofkn opened this issue Apr 24, 2014 · 5 comments
Labels

Comments

@Stofkn
Copy link

Stofkn commented Apr 24, 2014

Hi

I'm currently working with socket.io and the client sends a request but apperently the req.headers is undefined.

So in the index.js file on line 27, I would suggest to do a check if the request object has headers and then take cookie of the headers.
If there are no headers raise an exception and pass it to the callback.

Thanks!

    var cookies = req.headers.cookie;
                             ^
TypeError: Cannot read property 'cookie' of undefined
    at Array.cookieParser [as 0] (C:\nodejs\generator-rappit\test\temp\node_modules\cookie-parser\index.js:27:30)
    at run (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\lib\namespace.js:114:11)
    at Namespace.run (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\lib\namespace.js:126:3)
    at Namespace.add (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\lib\namespace.js:155:8)
    at Client.connect (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\lib\client.js:67:20)
    at Server.onconnection (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\lib\index.js:230:10)
    at Server.EventEmitter.emit (events.js:95:17)
    at Server.handshake (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\node_modules\engine.io\lib\server.js:242:8)
    at Server.handleRequest (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\node_modules\engine.io\lib\server.js:176:10)
    at Server.<anonymous> (C:\nodejs\generator-rappit\test\temp\node_modules\socket.io\node_modules\engine.io\lib\engine.io.js:119:14)
@dougwilson
Copy link
Contributor

This middleware is for parsing HTTP requests; the stack trace is coming from where socket.io is passing a socket object. This middleware is not a socket.io middleware, which is why you are having an issue.

@danpe
Copy link

danpe commented Apr 25, 2014

I'm also getting this 👍

@Stofkn
Copy link
Author

Stofkn commented Apr 25, 2014

My code to make it this cookie-parser module work together with socket.io:

// ### Cookie parser

// Wrapper arround Express cookie parser, so we can use the same cookie parser for socket.io.
// Parse Cookie header and populate `socket.request.cookies` with an object keyed by the cookie names.
// Uses signed cookies by passing a secret string, which assigns `socket.request.secret` so it may be used by other middleware.

function cookieParserWrapper (socket, next) {
  // request, response and callback
  cookieParser(socket.request, {}, next);
}`

I hope it will help someone else!

Kind regards
Kristof

@dougwilson
Copy link
Contributor

I don't know how to use socket.io, but this module requires an HTTP request as the first argument, and node.js core itself adds a headers property. If you are getting this error, you are simply using this module incorrectly with socket.io, because you are using it somewhere that is not giving it a HTTP request.

@rmethlie
Copy link

The cookie-parser reference you pass as a passportSocketio.authorize({...}) configuration attribute will get the app crypto secret when it is invoked, not the request instance you'd expect. Therefore, do not create an instance of the cookie-parser. Instead simply get a reference to the library and pass that in.

So don't do this

app.io.use(passportSocketIO.authorize({
    passport: passport,
    cookieParser: require('cookie-parser')(config.cryptoKey),
    ...

That results in the error you are seeing because the cookie-parser will now expect a request as an argument. Instead do this:

app.io.use(passportSocketIO.authorize({
    passport: passport,
    cookieParser: require('cookie-parser'), // dont init, let passport.socketio do that for you

Hope that helps.

@expressjs expressjs locked and limited conversation to collaborators Jun 22, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants