我收到未捕获的TypeError:“无法读取未定义的属性'0'”错误。我只是想不出这个问题。
尽管程序运行正常,但我得到了所需的输出。但是错误...
Console screenshot Console screeshot / resp object
a <- 1:5
b <- paste0("'", a, "'", collapse = ", ")
b
# [1] "'1', '2', '3', '4', '5'"
答案 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;
};