first I'm kind of new to JS so I hope my question is not trivial.
I' creating a dashboard for GPU Cluster Monitoring. The data is stored via JSON and I want to fetch the file and assign the dtata to a variable as JSObject, so I can use it further in the code.
Here a chunk of my data.json:
{
"time": [
"16:50:07",
"16:55:07"],
"node01": {
"cpu": [
"5",
"8"],
"mem": [
"7",
"5"],
"gpu": {
"g1": {
"gpu": [
"0",
"5"],
"mem": [
"2",
"8"]
}
}
}
}
And here is my JS Code using fetch
:
var obj;
fetch('http://.../data.json')
.then(res => res.json())
.then(res => {obj = res})
My Problem is that if I want to access for example obj.time[0]
I get the data in the browser console but if I try to access it in the script I get a Uncaught TypeError: obj is undefined
error. I hope for me some enlightenment!