So I have this react app made for a IoT project that as 3 charts with temperature and humity of soil and air, I make a fetch to my API witch returns a response JSON similar to this(But with 10 data values, I didn't include all to not make this post huge):
{
"dados":[
{
"T_air":20,
"H_air":60,
"soil":70,
"Time":"2019-12-12T16:09:35.719Z"
},
{
"T_air":20,
"H_air":60,
"soil":70,
"Time":"2019-12-12T16:09:35.719Z"
},
{
"T_air":20,
"H_air":60,
"soil":70,
"Time":"2019-12-12T16:09:35.719Z"
},
{
"T_air":20,
"H_air":60,
"soil":70,
"Time":"2019-12-12T16:09:35.719Z"
},
{
"T_air":20,
"H_air":60,
"soil":70,
"Time":"2019-12-12T16:09:35.719Z"
}
],
"LastTime":"12/12/2019, 15:43:53"
}
The point is I need to print the LastTime
object in the render, for that I use the setState
property but if console.log the ResData I can see my object, however if I console.log results
It says undefined.
You can see in my code that if I use ResData.dados[i].T_air
it gives no problem at all, so It's not the API fault.
import React, { Component } from 'react';
import '../App1.css'
import CanvasJSReact from '../assets/canvasjs.react';
var CanvasJSChart = CanvasJSReact.CanvasJSChart;
var dataPoints =[];
var dataPoints_1 = [];
var dataPoints_2 = [];
class LineChart extends Component {
constructor(props){
super(props);
this.state={
results: []
};
}
.
.
.
.
componentDidMount(){
var chart = this.chart;
var chart1 = this.chart1;
var chart2 = this.chart2;
fetch('http://localhost:4000/espdata/')
.then((response) => {
return response.json();
})
.then((ResData) => {
for (var i = 0; i < 10; i++) {
var today = new Date(ResData.dados[i].Time);
dataPoints.push({
x: today,
y: ResData.dados[i].T_air
});
dataPoints_1.push({
x: today,
y: ResData.dados[i].H_air
});
dataPoints_2.push({
x: today,
y: ResData.dados[i].soil
});
}
chart.render();
chart1.render();
chart2.render();
this.setState({results: ResData.LastTime});
console.log(this.results); //UNDEFINED
console.log(ResData.LastTime) // "12/12/2019, 15:43:53"
});
}
.
.
.
.
{this.state.results}
and it prints now the time! I feel dumb by not realising I had to use this.state. Thx a lot – PmmZenha Dec 12 '19 at 16:19