R
R
rovied2015-09-14 19:20:05
JavaScript
rovied, 2015-09-14 19:20:05

Why is an empty array coming from nodejs server?

Server

var players = [ uryBa10dquiRVP77AAAD: { id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 },
  PPF6HbYRf3dm30w1AAAE: { id: 'PPF6HbYRf3dm30w1AAAE', x: 200, y: 200 } ];

socket.broadcast.emit('updatePlayers', players);

client
console.log(players);
Result in console:
[]
Why is an empty array coming? Is this some feature of nodejs?
This is how it works
var players = { id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 };

socket.broadcast.emit('updatePlayers', players);

client
console.log(players);
Result in console:
Object {id: "5bHG5dipHpSBVFilAAAB", x: 200, y: 200}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Prozorov, 2015-09-15
@Staltec

You need to decide.
Or like this (associative array):

var players = { 
  uryBa10dquiRVP77AAAD: { id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 },
  PPF6HbYRf3dm30w1AAAE: { id: 'PPF6HbYRf3dm30w1AAAE', x: 200, y: 200 } 
};

Or like this (array of objects, as collection):
var players = [
  { id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 },
  { id: 'PPF6HbYRf3dm30w1AAAE', x: 200, y: 200 } 
];

But not like what you wrote.

D
Deodatuss, 2015-09-14
@Deodatuss

json send

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question