question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection shutdown throwing Uncaught TypeError: Cannot read property 'sid' of undefined

See original GitHub issue

[x] report a bug [ ] request a feature

Current behaviour

When initiating a connection, if there’s a rejection (I.E.: WebSocket is closed before the connection is established), the connection is shut down and the following error message is shown:

Uncaught TypeError: Cannot read property 'sid' of undefined
 at Socket.onpacket (socket.js:189)
    at Manager.<anonymous> (index.js:21)
    at Manager.Emitter.emit (index.js:145)
    at Manager.ondecoded (manager.js:209)
    at Decoder.<anonymous> (index.js:21)
    at Decoder.Emitter.emit (index.js:145)
    at Decoder.add (index.js:117)
    at Manager.ondata (manager.js:201)
    at Socket.<anonymous> (index.js:21)
    at Socket.Emitter.emit (index.js:145)

Steps to reproduce

I have simply instantiated a new connection with a WebSocket server with default settings passing a custom namespace.

Expected behaviour

I expected this variable to have some check to see if it’s undefined to prevent this error.

Setup

  • OS: Linux Mint 19
  • browser: Tested in Google Chrome Version 87.0.4280.66 (Official Build) (64-bit)
  • socket.io version: 3.0.3

Other information

To fix, change the following code on socket.ts file on line 227, adding the optional chaining operator to validate the object:

// socket.ts - line 227

 case PacketType.CONNECT:
        const id = packet?.data?.sid;
        this.onconnect(id);
        break;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
darrachequesnecommented, Dec 3, 2020

Hi, it seems you are trying to reach a Socket.IO server in v2 with a client in v3, but they are not compatible.

We will add a check for this, as this is a common question: https://github.com/socketio/socket.io-client/commit/dddd63a48f52d558a96accfc7025e9a25b2bfc6a

0reactions
darrachequesnecommented, Dec 7, 2020

An error will now be emitted when reaching a v2.x server: https://github.com/socketio/socket.io-client/commit/ec1f8c3474b54600420487a0554cb195cc78f2bc (included in socket.io-client@3.0.4).

I think this issue can now be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'sid' of undefined
The callback of client.messages.create is used as }, function(err, message) { console.log(message.sid); });. When there will be error, the first parameter ...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
Error and Warning Dictionary | Twilio
Below is a full list of all possible Twilio REST API error codes. Read our guidance on debugging your Twilio application for general...
Read more >
How to Prevent the Error: Cannot Read Property '0' of Undefined
A guide on how to prevent the error "cannot Read Property '0' of Undefined", covering techniques such as try, catch, using const over...
Read more >
Avoiding those dang cannot read property of undefined errors
​​​​ Uncaught TypeError: Cannot read property 'foo' of undefined. ​ The dreaded error we all hit at some point in JavaScript development. Could ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found