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

fix middleware initialization #2969

Merged
merged 1 commit into from
Jun 12, 2017

Conversation

darrachequesne
Copy link
Member

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behaviour

New behaviour

Fix TypeError: Cannot convert undefined or null to object when a middleware is added before the engine is properly attached.

Other information (e.g. related issues)

Closes #2963.

@ghost
Copy link

ghost commented Jun 13, 2017

/data/project/app/node_modules/on-headers/index.js:25
	throw new TypeError('argument res is required')
	^

TypeError: argument res is required
	at onHeaders (/data/project/app/node_modules/on-headers/index.js:25:11)
	at session (/data/project/app/node_modules/express-session/index.js:219:5)
	at Array.<anonymous> (/data/project/app/modules/webserver.js:41:5)
	at run (/data/project/app/node_modules/socket.io/lib/namespace.js:123:11)
	at Namespace.run (/data/project/app/node_modules/socket.io/lib/namespace.js:135:3)
	at Namespace.add (/data/project/app/node_modules/socket.io/lib/namespace.js:163:8)
	at Client.connect (/data/project/app/node_modules/socket.io/lib/client.js:76:20)
	at Server.onconnection (/data/project/app/node_modules/socket.io/lib/index.js:398:10)
	at emitOne (events.js:115:13)
	at Server.emit (events.js:210:7)
error: Forever detected script exited with code: 1
error: Script restart attempt #63

const session = require('express-session');
[..]
const httpsServer = https.createServer(credentials, app).listen(config.server.port);
const io = require('socket.io')(httpsServer);

var sessionMiddleware = session({
    store: new RedisStore({}),
    secret: 'MYSECRET',
    resave: false,
    saveUninitialized: true
});

io.use(function (socket, next) {
    sessionMiddleware(socket.request, socket.request.res, next); //(/data/project/app/modules/webserver.js:41:5)
});

this error occurred the first time after updating from 2.0.2 to 2.0.3 - any ideas?

@ToddAlvord
Copy link

Gindu's error occurs for me on any version of v2. I searched for the new location of res and could not find it.
sessionMiddleware( socket.request, {}, next );
The above line works and you still have session data, but you cannot modify or save the session this way.

dzad pushed a commit to dzad/socket.io that referenced this pull request May 29, 2023
Fix "TypeError: Cannot convert undefined or null to object" when a
middleware is added before the engine is properly attached.
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

Successfully merging this pull request may close these issues.

2.0.2 come across breakchange
2 participants