Javascript React Native TypeError:无法读取属性';项目名称';未定义的
我不知道为什么if条件中的代码仍在被解析,尽管它满足条件,但应该跳过它。this.state.projectInfo只是一个空数组([]) 不确定为什么TypeError:无法读取未定义的属性“projectName”仍作为错误返回。任何帮助都将不胜感激Javascript React Native TypeError:无法读取属性';项目名称';未定义的,javascript,react-native,typeerror,Javascript,React Native,Typeerror,我不知道为什么if条件中的代码仍在被解析,尽管它满足条件,但应该跳过它。this.state.projectInfo只是一个空数组([]) 不确定为什么TypeError:无法读取未定义的属性“projectName”仍作为错误返回。任何帮助都将不胜感激 renderProject(i){ if(this.state.projectInfo !=='undefined'){ return( <View>
renderProject(i){
if(this.state.projectInfo !=='undefined'){
return(
<View>
<Text>You are going to list the following project</Text>
<Panel
style={styles.thirdHeaderContainer}
header={this.state.projectInfo[i].projectName}>
<View style={{ flexDirection: "row" }}>
<Image style={{
width: 60,
height: 60,
}}
source={require('./robotics.png')} />
<View style={{ flexDirection: "column" }}>
<Text>
{this.state.projectInfo[i].faculty}
</Text>
<Text>
Listed by: John Smith
</Text>
<Text>
Collaborators: {this.state.projectInfo[i].collaborators}
</Text>
</View>
</View>
<View style={{ flexDirection: "row" }}>
<Text>This project requires skills that you have: </Text>
<Image style={{
width: 20,
height: 20
}}
source={require('./robotics.png')} />
</View>
<View style={{ flexDirection: "row" }}>
<Text>This project is suitable for your faculty: </Text>
<Image style={{
width: 20,
height: 20
}}
source={require('./computing.png')} />
</View>
<Text style={{ padding: '10' }}>Project Details:</Text>
<Text>
{this.state.projectInfo[i].projectDetails}
</Text>
<Button
variant="contained"
color="primary"
size='medium'
onClick={() => this.setState({ showAlert: true })}>
{this.state.projectInfo[i].expressInterest}
</Button>
<ModalEnhanced
showAlert={this.state.showAlert}
closeAlert={() => this.setState({ showAlert: false })}
text={this.state.projectInfo[i].alertText}
/>
</Panel>
</View>
)
}else{
return (
<View>
<Text>GO HOME</Text>
</View>
)
}
renderProject(一){
if(this.state.projectInfo!=='undefined'){
返回(
您将列出以下项目
{this.state.projectInfo[i].faculty}
列名人:约翰·史密斯
协作者:{this.state.projectInfo[i].Collaborators}
此项目需要您具备以下技能:
本项目适合您的教师:
项目详情:
{this.state.projectInfo[i].projectDetails}
this.setState({showarter:true}}>
{this.state.projectInfo[i].expressInterest}
this.setState({showarter:false})
text={this.state.projectInfo[i].alertText}
/>
)
}否则{
返回(
回家吧
)
}
条件验证并不是实际验证要使用的对象
this.state.projectInfo[i].projectName
如您所见,这里使用变量“i”,因此您应该更新条件验证来验证它
比如:
嗨,谢谢你的回答。我确实根据您的答案将条件更改为if(this.state.projectInfo!=='undefined'&&this.state.projectInfo[I]!=='undefined'),但它返回相同的错误。我对条件的定义有误吗?
if(this.state.projectInfo && this.state.projectInfo[i]){
...
}else{
...
}