无法读取未定义的 属性 0

Can not read property 0 of undefined

I get Uncaught TypeError: "Cannot read property '0' of undefined" error. I just can't figure out the issue.

虽然程序 运行 很好,但我得到了想要的输出。但是错误...

Console screenshot Console screeshot / resp object

$(window).bind("load", function() {

var ws = new WebSocket("wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD");

ws.onopen = function(){
  ws.send(JSON.stringify({"trade":"XBTUSD"}))
};

ws.onmessage = function (msg){
    var resp = JSON.parse(msg.data);
    console.log(resp);  

    var price = resp['data'][0].price; // can not read property 0 of undefined :/

    console.log('Price is : ' + price);


};

});

检查 resp/resp.data 是否为空。如果 resp/resp.data 为空,则索引 0.

处没有任何内容
   ws.onmessage = function (msg){
       var resp = JSON.parse(msg.data);
       console.log('Data : ' + data);  
       console.log('resp: ' + resp);  
       var price;
       if(resp && resp.data){
          price = resp.data[0].price; // can not read property 0 of undefined :/
       }

       console.log('Price is : ' + price);

      //document.getElementById('btcPrice').value = price;

   };