bigyellow
bigyellow

Reputation: 1

I'm having issues passing an array from my node.js app to my javascript client

I have live chat on my website and right now it's just polling. I want to get with the times and replace this with a node.js version. I've been making good progress but am now stuck on something that appears to just be a syntax issue.

So what I'm doing is when the user first comes to my site, I want to show them the most recent chat that's in the mysql database. So it will be rows of

time user their message

I'm sending this data on user connect from my app.js file with

io.sockets.on('connection', function (socket) {
//Make connection to mysql 
connection.query('select values from my_table limit 5', function(err, rows, fields) {
user_info = new Array();
for (i = 0; i < rows.length; i++) {
  user_info[i] = new Array();
  user_info[i]['time'] = rows[i].time;
  user_info[i]['user'] = rows[i].user;
  user_info[i]['their_message'] = rows[i].their_message;
}
io.sockets.emit('some_event_tag', user_info);
});

So this works, except that when I try and access this data under the function associated with "some_event_tag", it appears my syntax is off because I'm not getting the information. So on the client, I'm trying to access the data with

some_event_tag: function(data) {
  var msg = '';
  for (i = 0; i < data.length; i++) {
    msg = $('<div class="msg"></div>')
      .append('<span class="name">' + data[i]['user'] + '</span>: ')
      .append('<span class="text">' + data[i]['their_message'] + '</span>');
      $('#messages')
        .append(msg);
    msg = '';
  }
},

but for whatever reason I'm getting "undefined". On the server side, if I change

io.sockets.emit('some_event_tag', user_info);

to something like

io.sockets.emit('some_event_tag', user_info[0]['user']);

I can access this value on the client (by just saying "data"). Of course, in this case, I'm only passing one value. Not what I want. On the client side, I can also correctly see that five array elements are being passed. In other words, data.length is correctly set to 5. However, I can't actually figure out the syntax to access the data on the client side. Isn't this just typical javascript arrays? I'm a little stumped at this point and google didn't help me this time. Any help you guys can give would be greatly appreciated.

Upvotes: 0

Views: 925

Answers (2)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123463

JavaScript has a rather strong assumption that Arrays use numeric keys between 0 and length-1. While they can still have other keys, most functions for handling Arrays will ignore them -- including JSON.stringify() that Socket.IO uses.

So, if you need to set other keys and don't want them skipped, you'll want to use a plain Object instead:

var user_info = new Array();
for (var i = 0; i < rows.length; i++) {
  user_info[i] = new Object();
  // ...
}

Upvotes: 2

autistic
autistic

Reputation: 15642

I suspect producing an SSCCE would highlight your issue, among others:

// The time is the user and the user is the time? ;)
user_info[i]['time'] = rows[i].user;
user_info[i]['user'] = rows[i].time;

If rows[i].time is something date-like, then user_info[i]['user'] is likely to be something date-like. A question arises as to whether or not io.sockets.emit can emit date-like things:

io.sockets.emit('some_event_tag', user_info[0]['user']);

There appears to be no documentation for io.sockets.emit. That's not entirely helpful, is it? Ohh well. I'm confident. Keep us updated, right?

Upvotes: 0

Related Questions