J.Jay
J.Jay

Reputation: 249

Socket.io error in sending arrays

I have 3 variables to send to the client using socket.io, namely, mapRes & location.

server.js

var mapRes = {width : 720, height: 1040};
var location = [{x:100,y:100,z:100},{x:200,y:200,z:200}];
var obj = {'1':{x:100,y:200}}, '2':{x:200,y:100}};
io.on("connection", function(socket){
    socket.emit("mapRes",mapRes);
    socket.emit("location",location);
    socket.emit("object",obj);
}

client.html

socket.on('mapRes',function(message){
    var mapRes = message;
    console.log(mapRes);
});
socket.on('location',function(message){
    var location = message;
    console.log(location);
});
socket.on('object',function (message){
    var object = message;
    console.log (object);
});

At the client side (browser), the first variable mapRes is received as expected.

Object { width: 720, height: 1040 }

The second variable, however, is received as shown below:

Array [ Object, Object]

The third object is received as below:

Object { 1: Object, 2: Object}

Is there a way to receive these variables/objects properly?

Upvotes: 0

Views: 130

Answers (1)

tom
tom

Reputation: 692

It probably has been sent correctly, you're just not seeing the whole object.

This is due to Firefox collapsing objects when logged to console:

firefox console

If you click on the underlined object, the right panel should open and show you more details about the object.

Upvotes: 1

Related Questions