我正在從服務器接收單個學生的json數據。在這裏,我可以很容易地得到this.state.info.Firstname,但我無法訪問this.state.info.Father.Firstname。我怎樣才能訪問?無法訪問反應中的數組元素
這是我的反應代碼:
import React from 'react';
import axios from 'axios';
class Information extends React.Component{
constructor(props){
super(props);
this.state={
info:' '
}
}
componentDidMount(){
let self = this;
axios.get('http://localhost:8080/studentById')
.then(function(data) {
//console.log(data);
self.setState({info:data.data});
});
}
render(){
return(
<div>
<pre>
<ul>
<li>{this.state.info.Firstname}</li>
<li>{this.state.info.Lastname}</li>
<li>{this.state.info.DateOfBirth}</li>
<li>{this.state.info.PlaceOfBirth}</li>
<li>{this.state.info.Age}</li>
<li>{this.state.info.Months}</li>
<li>{this.state.info.Nationality}</li>
<li>{this.state.info.MotherTongue}</li>
<li>{this.state.info.BloodGroup}</li>
<li>{this.state.info.Father.Firstname}</li>
</ul>
</pre>
</div>);
}
}
export default Information;
這是我的JSON數據:
{ _id: 5899b77c0ce1d10b723ab4ac,
Id: 92,
Firstname: 'Surya',
Age: 11,
Lastname: 'G',
DateOfBirth: '11-11-11',
PlaceOfBirth: 'Bangalore',
Months: 2,
Nationality: 'Indian',
MotherTongue: 'Kannada',
BloodGroup: 'B+ve',
ResidentialAddress: 'Vijayanagar 3rd cross, Bangalore',
EmergencyContactNumber:
{ Address: 'SIT',
PhoneNo1: 1234,
PhoneNo2: 123455,
Relationship1: 'Uncle',
Relationship2: 'Aunty' },
Mother:
{ Firstname: 'Joe',
Lastname: 'S',
Occupation: 'Business',
PlaceOfWork: 'Bangalore',
OfficialAddress: 'Jayanagar',
EmailId: '[email protected]',
PhoneNo: 12345,
MobileNo: 1234567890 },
Father:
{ Firstname: 'Chandra',
Lastname: 'S',
Occupation: 'Business',
PlaceOfWork: 'Bangalore',
OfficialAddress: 'BTM',
EmailId: '[email protected]',
PhoneNo: 12345,
MobileNo: 1234567890 } }
如果使用'this.state.info.Father.Fullname'時出現任何錯誤? –
yes..like我得到:不能讀取屬性'名字'的undefined –