How to resolve TypeError: Cannot read property 'connected' of undefined
See original GitHub issueHi,I am getting the following error while using socket.io version 1.3.5
with node.js for my app.
error:
Missing error handler on `socket`.
TypeError: Cannot read property 'connected' of undefined
at Socket.<anonymous> (C:\Documents and Settings\Subhrajyoti\Desktop\Restore
\htdocs\video broadcast\server.js:68:13)
at Socket.emit (events.js:95:17)
at Socket.onevent (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htd
ocs\video broadcast\node_modules\socket.io\lib\socket.js:330:8)
at Socket.onpacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
docs\video broadcast\node_modules\socket.io\lib\socket.js:290:12)
at Client.ondecoded (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\h
tdocs\video broadcast\node_modules\socket.io\lib\client.js:193:14)
at Decoder.Emitter.emit (C:\Documents and Settings\Subhrajyoti\Desktop\Resto
re\htdocs\video broadcast\node_modules\socket.io\node_modules\socket.io-parser\n
ode_modules\component-emitter\index.js:134:20)
at Decoder.add (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdocs
\video broadcast\node_modules\socket.io\node_modules\socket.io-parser\index.js:2
47:12)
at Client.ondata (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdo
cs\video broadcast\node_modules\socket.io\lib\client.js:175:18)
at Socket.emit (events.js:95:17)
at Socket.onPacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
docs\video broadcast\node_modules\socket.io\node_modules\engine.io\lib\socket.js
:99:14)
And server file is given below.
server.js.
var port=8888;
var express=require('express');
var morgan = require('morgan');
var http=require('http');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var mongo = require('mongojs');
var database='Oditek';
var collections=['video'];
var app= express();
var server=http.Server(app);
var io=require('socket.io')(server);
var db = mongo.connect("127.0.0.1:27017/"+database, collections);
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
db.on('ready', function () {
console.log('database connected')
});
app.get('/',function(req,res){
res.sendfile('view/login.html');
});
app.post('/login',function(req,res){
var username=req.body.username;
var password=req.body.userpassword;
if(username && password){
db.video.findOne({
username:username,
password:password
},function(err,doc){
if(doc){
console.log('login',doc);
res.send(doc);
}
if(err){
console.log('login12',err);
res.send("could not login");
}
});
}
});
app.get('/video',function(req,res){
res.sendfile('view/video.html');
});
//socket----programming//
var roomid;
var clients={};
io.sockets.on('connection',function(socket){
//console.log(socket);
roomid=socket.handshake.query.roomid;
var usertype=socket.handshake.query.usertype;
socket.on('admin-join',function(data){
console.log('admin',data);
if(data.IsJoinAdmin){
clients={
"socket":roomid
}
socket.join(roomid);
}
});
socket.on('user-join',function(data){
console.log('user',data);
if(data.isUserJoin){
io.sockets.connected[clients.socket].emit('user-already-joined',data);
socket.join(roomid);
}
});
socket.on('send-broadcasting-message',function(data){
console.log('message',data)
io.to(roomid).emit('sending-broadcasting',data);
});
});
server.listen(port);
console.log('server is listening on the port'+port);
Please help me to resolve this error.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
node.js - TypeError: Cannot read property 'connect' of undefined
Correct way var MongoClient = require('mongodb').MongoClient; // it's not a function. Your way var MongoClient = require('mongodb').
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 >Uncaught TypeError: Cannot read property of undefined In
Uncaught TypeError: Cannot read property of undefined error occurs in Chrome when you read a property or call a method on an undefined...
Read more >How to resolve a 'cannot read property connect of undefined ...
You get this error because the variable object is not defined, and trying to retrieve, set or mutate the property “id” on undefined...
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you should use io.to(socketid).emit(‘user-already-joined’,data) instead of io.sockets.connected[clients.socket].emit(‘user-already-joined’,data); about more,you can see my blog :http://blog.csdn.net/zls986992484/article/details/71599297
@zzxBear none of socketio’s problom