试图访问已经.emit和无法访问的数组数据

时间:2016-03-31 21:06:20

标签: javascript arrays socket.io

在我的网站上加载我正在查询数据库并检索行。

但是我无法访问此comment_data

function onLoad(socket) {
    var connection = mysql.createConnection(db_access);
    connection.connect(function(err){
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });

    var q = "SELECT * FROM (SELECT * FROM chat ORDER BY id DESC LIMIT 8) chat ORDER BY id ASC";

    connection.query(q, function (err, results) {
        if (err) throw err;

        //io.sockets = emit to all
        io.sockets.emit("onLoad_comment", {
            load_comment_data: new Array(results[0],results[1],results[2],results[3],results[4],results[5],results[6],results[7])
        });

        connection.end();
    });
}

我用

处理数据
socket.on('onLoad_comment', function (data) {
    for (i=0; i< data.load_comment_data.length; i++) {
        alert(data.load_comment_data[0].nick);
        //var content = '<div class="msgln"><div class="chat_prof_pic"><a href="' + data.comment_data[i].url + '"><img src="' + data.comment_data[i].pic + '"></a></div><div class="chat_msg"><b>' + data.comment_data[i].nick + '</b>';
        //content += ' ' + data.comment_data[i].comment + '</div></div><hr>';
        //$('#chatbox').append(content);
        //content = '';
    }
});

我试过了

  1. data.load_comment_data [0] .nick
  2. data.load_comment_data [0] [0]
  3. 但是我只是无法访问这些数字..但是,将for循环中的data.load_comment_data.length作为一部分返回正确的数字,以便&#39;某些东西&#39;正在发送。

    如果我这样做

    console.log(Array(results[0],results[1],results[2],results[3],results[4],results[5],results[6],results[7]));
    

    我明白了:

    [ { id: 3,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n3',
        created_at: Thu Mar 31 2016 10:49:51 GMT-0700 (PDT) },
      { id: 4,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n4',
        created_at: Thu Mar 31 2016 10:49:51 GMT-0700 (PDT) },
      { id: 5,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n5',
        created_at: Thu Mar 31 2016 10:49:52 GMT-0700 (PDT) },
      { id: 6,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n6',
        created_at: Thu Mar 31 2016 10:49:52 GMT-0700 (PDT) },
      { id: 7,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n7',
        created_at: Thu Mar 31 2016 10:49:52 GMT-0700 (PDT) },
      { id: 8,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: '\n8',
        created_at: Thu Mar 31 2016 10:49:53 GMT-0700 (PDT) },
      { id: 9,
        sender: 'K1LzR',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d8/d85a3b326846fa2b0f6e59acdbaa0cfef307bd87_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198124567784/',
        message: 'test',
        created_at: Thu Mar 31 2016 11:25:16 GMT-0700 (PDT) },
      { id: 10,
        sender: 'Denilsonjvv',
        sender_pic: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg',
        sender_url: 'http://steamcommunity.com/profiles/76561198294181499/',
        message: 'fesfse',
        created_at: Thu Mar 31 2016 12:48:56 GMT-0700 (PDT) } ]
    

1 个答案:

答案 0 :(得分:1)

结果数据中没有nick。请尝试使用sender