Hello I am writing an angular app. I have a Node API returning data which I am trying to chart using Chart.js. I am getting my data from the API and it returns an object like this:
{
"0": {
"_id": "62716061f6897b637f59ba9e",
"created": "2022-05-03T17:03:29.459Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.42499542,
"metric": 19.0625,
"controlStatus": false,
"_id": "62716061f6897b637f59ba9f"
},
{
"name": "Outside Air Temperature",
"imperial": 65.75,
"metric": 18.6875,
"controlStatus": false,
"_id": "62716061f6897b637f59baa0"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62716061f6897b637f59baa1"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa2"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa3"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa4"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa5"
}
],
"__v": 0
},
"1": {
"_id": "62715f33f6897b637f59ba92",
"created": "2022-05-03T16:58:27.064Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.3125,
"metric": 19.0625,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba93"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba94"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba95"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba96"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba97"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba98"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba99"
}
],
"__v": 0
},
"2": {
"_id": "62715e04f6897b637f59ba72",
"created": "2022-05-03T16:53:24.706Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.19999695,
"metric": 19.0625,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba73"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba74"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba75"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba76"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba77"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba78"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba79"
}
],
"__v": 0
},
"3": {
"_id": "62715cd6f6897b637f59ba66",
"created": "2022-05-03T16:48:22.086Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.19999695,
"metric": 19,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba67"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba68"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba69"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6a"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6b"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6c"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6d"
}
],
"__v": 0
}
}
I am trying to iterate over this to parse my data points but I am getting errors. Here is my code:
constructDataSet(input: IGreenhouseData[]) {
let labels: string[] = [];
let imperial: number[] = [];
let metric: number[] = [];
let datasets: IDataSet[] = [];
for (let item of input) {
for (let dataPoint of item.items) {
labels.push(dataPoint.name);
imperial.push(dataPoint.imperial);
metric.push(dataPoint.metric);
}
let newDataSet: IDataSet = {
label: 'Imperial',
data: imperial,
};
datasets.push(newDataSet);
newDataSet = {
label: 'Metric',
data: metric,
};
datasets.push(newDataSet);
}
this.data = {
labels: labels,
datasets: datasets,
};
}
This is the error: ERROR TypeError: input is not iterable
CodePudding user response:
input
is not an array, but an object. Change the for-loop to:
for (let item of Object.values(input)) {
/*rest of the code*/
}
so the iteration is done over the object values rather than the object itself.
CodePudding user response:
As stated by @Nonik and @NicholasK the input is an Object not an array. I need to fix that. Thank you both!