#1
  1. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    62

    Problems passing an array in my node application


    Hey guys. I'm creating an app in node that initially grabs data from my database and attempts to display it to the user when they first come to the page. It's a chat application that I'm creating to replace the one on my site now, which does polling.

    I'm new to node and was making good progress until a ran into a little bit of a brick wall, over something that seems like it should be easy. So here's the code that's stumping me.

    Code:
    //client code
        some_event_name: 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]['message'] + '</span>');
              $('#messages')
                .append(msg);
            msg = '';
          }
        },
    
    //server code that runs when the person first comes to the page
      connection.query('select user, message, submission_datetime from my_table order by submission_datetime desc limit 5', function(err, rows, fields) {
        if (err) throw err;
        user_info = new Array();
        for (i = 0; i < rows.length; i++) {
          user_info[i] = new Array();
          user_info[i]['user'] = rows[i].user;
          user_info[i]['message'] = rows[i].message;
          user_info[i]['submission_datetime'] = rows[i].submission_datetime;
        }
        io.sockets.emit('some_event_name', user_info);
    So when the user first comes to the page, the server grabs data from the database and sends it to the client via the function connected to "some_event_name". This then loops through the array and creates divs, one for each database row returned. As written above, I don't seem to be able to pass "user_info" from the server to the client. Well I'm able to pass it, but not reference it correctly. However, I can pass simple data. So for the server, if I instead say

    Code:
    io.sockets.emit('some_event_name', user_info[0]['user']);
    I can get that data on the client side by just referencing it as "data" on the client side. When I try and pass an array on the server side, however (which is what I want to do), on the client side I can correctly see that data.length is equal to 5, but if I attempt to reference any of the data in the array with things like data[i]['user'] I just land up with "undefined". I tried playing with the syntax, but wasn't able to get anything to work. Seems like this should be simple. I'm passing an array and the client is correctly able to see that it has five elements. How do I actually reference the values in the array? Thanks in advance for any help you can give.
    Correspondence chess
    nothingbutchess.com
  2. #2
  3. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    62
    Ok, I found something that works. Apparently you can't pass arrays with emit. Or maybe you can but I just haven't figured out how to do so. In any event, instead of trying to pass

    Code:
    io.sockets.emit('some_event_name', user_info);
    where user_info was that array, I instead set user_info to a json string representing the array. Then, on the client side I eval'd the data parameter, which turns it into a json object, then looped through it as before and accessed the values with

    data[i].user and data[i].message

    If you don't want to use eval, you can use

    data = JSON.parse(data);

    but from what I understand, this fails in some of the older browsers. You can get around this by including a json parser and using the parser function if the browser is old, but since I'm sanitizing the data from the server, I trust it and I think I'll use eval for now. We'll see. Anyway, hopefully if someone else runs into the same issue I did they don't spend four days banging their head!
    Correspondence chess
    nothingbutchess.com

IMN logo majestic logo threadwatch logo seochat tools logo