React native 在React本机应用程序中显示数据库数据时遇到问题
我是英语初学者。在我的应用程序中,我从php后端获取json编码的数据。我毫无问题地获取了数据,并在控制台中进行了尝试。它工作得很好 这是我获取数据的代码React native 在React本机应用程序中显示数据库数据时遇到问题,react-native,React Native,我是英语初学者。在我的应用程序中,我从php后端获取json编码的数据。我毫无问题地获取了数据,并在控制台中进行了尝试。它工作得很好 这是我获取数据的代码 componentWillMount() { fetch('http://bsthisarasinghe-001-site1.1tempurl.com/projectDetails.php', { method: 'POST', headers: { 'Accept': 'app
componentWillMount() {
fetch('http://bsthisarasinghe-001-site1.1tempurl.com/projectDetails.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
job_code: this.props.navigation.state.params.code
})
}).then((response) => response.json())
.then((responseJson) => {
this.setState({ data: responseJson.results });
}).catch((error) => {
Alert.alert("No internet connection");
this.setState({ loading: false });
});
}
这就是我在控制台中打印它的方式
onFocus1() {
console.log(this.state.data[0].Job_Code);
}
但当我试图在应用程序中显示相同的字符串时,它显示了一个错误
我试过这个
<View style={{ width: '35%' }}>
<Text style={styles.textBoldStyle}>Project Name: </Text>
</View>
<View style={{ width: '65%' }}>
<Text style={styles.textStyle}>{this.state.data[0].Job_Code}</Text>
</View>
项目名称:
{this.state.data[0]。作业代码}
然后它说
“TypeError:无法读取未定义的属性'Job_Code'”
我想创造的是这个
我想将数据库数据分别添加到那些键入“某物”的地方。
请帮助我。我认为,在显示数据之前,您应该先检查数据是否已加载,如this.state.data.length>0,然后…数据已加载。我查过控制台我没说你的数据没有加载。在每个setState方法之后调用组件的渲染。但是,在加载数据并调用setState之前,您的组件将装载一个空数据。所以,你应该在渲染方法中控制你的数据大小。哦,我明白了。我会尝试添加一个加载屏幕,直到加载数据为止