2
I have a API
(POST
) in which I pass some parameter and it returns me results and the question is the following:
I got a guy Dashboard where I have to take DIA data and MES data (using the same API, but passing different parameters.
To get the data of the day I use:
import React, {Component} from 'react'
import '../../index.css';
export default class FaturamentoDia extends Component{
constructor(props) {
super(props);
this.state = {
itensDia: [],
wTotGeral: '',
};
}
componentDidMount() {
const data = {
"aEmp" : ["004"],
"PeriodoDe" : "2019-1-15T12:54:21Z",
"PeriodoAte" : "2019-1-15T12:54:21Z"
}
const requestInfoDia = {
method: 'POST',
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
}),
};
const dadosDia = fetch('http://10.0.0.49:9000/rzcFatPeriodo', requestInfoDia);
dadosDia.then(response => {
console.log(response);
return response.json();
}).then(itensDia => {
var swTotGeral = 0;
for (var i=0;i<itensDia.length;i++){
if(itensDia[i].DEFFATURAMENTO === 'D'){
var wValAux = -1;
}else{
wValAux = 1;
}
if(itensDia[i].ESTADONF !== 'C' && itensDia[i].ESTADONF !== 'I' && itensDia[i].ESTADONF !== 'D' ){
swTotGeral = swTotGeral + (itensDia[i].VALCONTABIL* wValAux);
}
}
var wTotGeral = swTotGeral.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});
console.log("Dia: "+wTotGeral)
this.setState({ itensDia, wTotGeral })
});
}
render() {
const {wTotGeral} = this.state
return (
<div>
{wTotGeral}
</div>
);
}
}
The final result appears correctly in wTotGeral;
in another file I do the month .... changing only
const data = {
"aEmp" : ["004"],
"PeriodoDe" : "2019-1-01T12:54:21Z",
"PeriodoAte" : "2019-1-31T12:54:21Z"
}
that is taking data of the whole month, the problem that when I will show the data on the screen, a request returns 200 (ok) and shows the data and the other returns 500.
Could someone give me a hand on how to make the data appear at the same time and I tried await
before the fetch
, but it didn’t work out.
It sure helped a lot... I’ll try to work on that tip of yours.... but it left me a doubt ... in this way how I would search the data of my API ? (I am beginner in this area)
– Alexandre Prezzi
@Alexandreprezzi The service is assuming the responsibility of carrying out the requisition, as long as it receives the data, serving as an abstraction so that it can be used by one or more components, as happens in
const billingOfDay = await getBilling(billingOfDayRequestData);
.Services are usually defined in a separate directory in the project, often in/src/services
.– ljsb