2016-03-31 61 views
0

在我的網站上,我正在查詢數據庫並檢索行。嘗試訪問已發送且無法訪問的陣列數據

但是我無法訪問此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]

但我只是不能訪問數字..然而,作爲for循環的一部分data.load_comment_data.length返回正確的數字,所以'東西'正在發送。

,如果我做

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) } ] 
+0

當你創建對象的時候你把它稱爲'comment_data',然後你試圖通過'load_comment_data'來訪問它 – trex005

+0

對不起,不知道如何複製錯誤。它目前是兩個load_comment_data;否則for循環不會像atm那樣工作,它只會提醒undefined 8次 – Matt

+0

爲什麼不把它設置爲:'load_comment_data:results'? – trex005

回答

1

您沒有在搜索結果中的數據nick。請嘗試使用sender代替。