我将数据从我的websocket传递到数组中。我想将数组传递给document.getElementById
。最后,我想在div
中显示我的数据。我得到了null is not an object (evaluating 'document.getElementById('temp').innerHTML')
的JavaScript
socket.on('data_added', function(mess){
console.log(mess);
var x =[];
x.push(mess);
console.log(x);
var datetime = [];
var humidity = [];
var temp = [];
for(var i =0; i < x.length; i++)
{
datetime.push(x[i].datetime);
humidity.push(x[i].humidity)
temp.push(x[i].temperature)
}
temp = document.getElementById('temp').innerHTML ;
humidity = document.getElementById('humidity').innerHTML;
console.log(datetime);
console.log(temp);
console.log(humidity);
});
我的数据
[{datetime: "2017-01-09T02:07:38.490Z", temperature: 19, humidity: 23}]
我将不胜感激。
答案 0 :(得分:0)
抛出异常表示您的网页上没有标记为“临时”的标记。 我认为你需要写那样的smth:
socket.on('data_added', function(x){
if(typeof x === "string") {
x = JSON.parse(x);
}
var datetime = [];
var humidity = [];
for(var i =0; i < x.length; i++) {
datetime.push(x[i].datetime);
humidity.push(x[i].humidity)
}
document.getElementById('temp').innerHTML = datetime ;
document.getElementById('humidity').innerHTML = humidity;
console.log(datetime);
console.log(humidity);
});
&#13;
<div id="temp"></div>
<div id="humidity"></div>
&#13;