I am trying to update a JavaScript window object in JavaScript that gets triggered as part of WebSockets.
Here is my code:
addData(data) {
// window.chart.data.datasets.forEach((dataset) => {
// dataset.data.push(data);
// });
console.log(data)
console.log(window.chart.data.datasets[0].data)
window.chart.data.datasets[0].data.push(data)
console.log('-----------------')
console.log(window.chart.data.datasets[0].data)
console.log('-----------------')
window.chart.update();
}
I'm using JavaScript chart.js library. I will explain the lines one by one:
console.log(data)
logs the json data back from the server. In the attached image you can see it is {x: 47, y: 0.61, id: 31049}console.log(window.chart.data.datasets[0].data)
logs the original state of the data attribute of datasets[0]. In the log below, you can see it has 2 elements and log is prefixed with "(2)"- Then I do a push to JavaScript array stored in
...datasets[0].data
- Then I do the same log in step 2 above after I've pushed the Json from step #1 to the data attribute in step #2. This log shows the prefix of "(3)", which seems fine, but there are only 2 original elements and not 3. This is confusing.
- If I do the same steps manually from browser console, it all works fine. So it doesn't seem like there is anything wrong with the code.
Why isn't my json from step 1 is getting pushed to the array referenced by the data attribute of datasets[0]
when done through the WebSockets?