无法读取未定义的属性0

时间:2018-10-03 06:54:43

标签: javascript api bitmex

  

我收到未捕获的TypeError:“无法读取未定义的属性'0'”错误。我只是想不出这个问题。

尽管程序运行正常,但我得到了所需的输出。但是错误...

Console screenshot Console screeshot / resp object

a <- 1:5
b <- paste0("'", a, "'", collapse = ", ")
b

# [1] "'1', '2', '3', '4', '5'"

1 个答案:

答案 0 :(得分:1)

检查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;

   };