我正在从API中提取数据,该API会返回这样的数据
[
{
"id":466,
"createdDate":"2019-04-18 13:22:34",
"updatedDate":null,
"isAdmin":false,
"createGroupRequestsCount":0,
"createdGroupsCount":0,
"postsCount":0,
"community":{
"id":7,
"createdDate":"2019-04-18 13:14:34",
"avatar":"",
"name":"FAX International School Community",
"description":"",
"referenceId":6,
"referenceName":"Fax International School",
"referenceAlias":"FIS",
"createGroupRequestsCount":0,
"groupsCount":0,
"postsCount":0
},
"member":{
"id":501,
"firstname":"Jack",
"lastname":"Manson",
"othernames":null,
"createdDate":"2019-04-18 13:21:49",
"updatedDate":null,
"isEmployee":false,
"email":"ernest.agyekum@minex360.com",
"avatar":null,
"primaryContact":"+233244908666",
"referenceId":1112,
"postsCount":0
}
}
]
我使用以下脚本提取数据,该脚本也返回带有子对象的数据
componentDidMount = async () => {
const communities = await API.getUserCommunityInfo(userId,schoolId);
const groups = await API.getGroups(communityMemberId);
console.log(communities)
}
当我尝试使用member
访问子对象console.log(communities.member[0].id)
时,我收到一条错误消息cannot read property of 0 undefined
有关如何检索子对象的任何帮助吗?
答案 0 :(得分:3)
您收到错误消息,因为member
不是communities
的直接子代
communities
是Array
中的Objects
,因此,要访问第一个成员,您需要执行communities[0].member.id
。