I am storing each response entity in object then pushing them in array where case 1 is working while case 2 is not.
Case 1 dummy data from external JSON file:
const {addEvent} = this.props
var airportList= FlightInfo.Airport; //FlightInfo carries data in JSON
//file
var newEvents=[];
for(var rootKey in airportList){
if(rootKey===airportValueSelected)
{
airportList[rootKey].forEach(element => {
if(element.flight_no===flightValue){
for(var m=0;m<element.Events.length;m++){
var singleEvent={
event_name:'',
date_time:'',
status:'',
}
singleEvent.event_name=element.Events[m].event_name;
singleEvent.date_time=element.Events[m].date_time;
singleEvent.status=element.Events[m].status;
newEvents.push(singleEvent);
}
for(var s=0;s<element.Alerts.length;s++){
newAlerts.push(element.Alerts[s]["Alert"+(s+1)]);
}
}
});
addEvent(newEvents);
The console result from Case 2:
Above response if you look closely I am getting [{...}]
Now Case 2 from Server response
axios.get('http://localhost:6020/fetchData?name='+data1+'&flight_no='+data2+'&date='+data3)
.then(res=>{
console.log(res);
console.log(res.data.resultSet);
if(res.data.error==null || res.data.error===""){
res.data.resultSet.forEach(element => {
var singleEvent={
completionIntent:'',
Name:'',
flightno:'',
date:'',
audioFileName:'',
status:'',
};
singleEvent.airportName=element.airportName;
singleEvent.completionIntent=element.completionIntent;
singleEvent.audioFileName=element.audioFileName;
singleEvent.flightno=element.flightno;
singleEvent.departureDate=element.departureDate;
singleEvent.status='On-time';
newEvents.push(singleEvent);
});
}
}).catch((e)=>console.log("Can’t access"+e));
console.log(newEvents);
addEvent(newEvents);
The console result from Case 2:
Now in second case when I pass these newEvents to B(main) component and then to C component it becomes empty when I try to get value {this.props.events}
B Component:
export default class userLogin extends Component {
constructor() {
super();
this.state = {
events:[],
};
}
addEvent = newEvent => this.setState({
events: newEvent
});
render(){
const {events} = this.state
return(
<SearchFlight events={events} alerts={alerts} addAlert={this.addAlert} addEvent={this.addEvent} />
<Events events={events}/>
);
}
UPDATE: ADD EVENT COMPONENT
render(){
console.log("this event:"+this.props.events);
return(
<div style={{display:'block',}}>
<div className="row" >
{this.props.events.map((event) =>
(
<div className="col-xs-3 col-sm-3 col-md-3 col-lg-3" key={event.completionIntent} >
<Card style={{minWidth : '275px', backgroundColor:'#F5F5F5', marginBottom:'10px', marginRight:'2px',}}>
<CardContent>
<Typography style={{fontSize:'14px',}} color="textSecondary" gutterBottom>
<p style={{fontSize:'20px',width:'78px', marginBottom:'0px',float:'right',}} >{event.status}</p>
</Typography>
<Typography variant="h5" component="h2" style={{fontWeight:'bold',}}>
{event.airportName.toUpperCase()}
</Typography>
<Typography style={{marginBottom:'12px'}} color="textSecondary">
Event
</Typography>
<Typography component="p" style={{color: event.status==='On-time'?'#33691E':event.status==='Delayed'?'#D50000':'grey', fontSize: '12px',}}>
{event.departureDate}
<br />
</Typography>
</CardContent>
<CardActions>
<Button size="small">Download Audio {event.audioFileName}</Button>
</CardActions>
</Card>
</div>
))}
</div>
</div>
);
}
Problem: Getting empty props events when passing to Events component. Case 1 is working fine but I didn't change anything except add few more objects and it stops working