I'm having an issue sending an array from the backend js to the client side.
I've tried the following on the server side:
for (var i=0; i < clients.length; i++) {
clients[i].send(clients);
}
for (var i=0; i < clients.length; i++) {
clients[i].send(JSON.stringify(clients));
}
for (var i=0; i < clients.length; i++) {
clients[i].send(clients.join('\n')));
}
Unfortunately, none of the above solutions worked, The JSON.stringify method obviously didn't work on the serverside due to JSON.stringify being a browser method however the other methods returned either [object Object]
or "[object Object]"
How can I send the array clients
to client side, or even if I can encode it into JSON and then send it over and parse it on the client side.
Really all I need is to send the contents over to the client side, but I have no clue how to do so haha
Any ideas are appreciated :)
If you are using Nodejs, then the JSON object is available by default (it is built into V8 so Nodejs gets it for free).
The inverse of the JSON.stringify() method is JSON.parse().
For example:
> s = JSON.stringify([1,2,3]);
'[1,2,3]'
> a = JSON.parse(s);
[ 1, 2, 3 ]
If the server is sending the result of stringify, then the client must run parse to extract the original data and vice versa.
Although this isn't a real solution it is working for me for now unless I find a better way to do it.
Using the .toString()
method:
for (var i=0; i < clients.length; i++) {
clients[i].send(clients.toString());
}
and then interpreting that output on the client side with this
var clients = string.split(',');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With