[英]React Native: Error:"Cannot read property 'name' of undefined, " Although object which contains name is not undefined
I am new to react and working on a project in react native.我是新来的反应和反应原生项目。 I have loaded data from the server using fetch, Data is available, if i console it shows up, but when i render the data it show undefined.我已经使用 fetch 从服务器加载数据,数据可用,如果我控制台它显示,但是当我呈现数据时它显示未定义。 Being from angular background i know the data is available late for rendering, if i put some conditions then the error will go away.来自 angular 背景,我知道数据可用于渲染,如果我提出一些条件,那么错误就会消失。 What i want is not to put condition on every single variable, What is the better way to manage it like we use " *ngIf
" in Angular on whole content, show only if data is available.我想要的不是对每个变量都设置条件,有什么更好的方法来管理它,就像我们在Angular 中对整个内容使用“ *ngIf
”一样,仅在数据可用时显示。 what i tried so far is below.到目前为止我尝试过的内容如下。
Fetching Data获取数据
componentDidMount() {
this._fetchData();
}
_fetchData = () => {
if (this.state.loading) { return; }
this.setState({ loading: true });
let typr = this._returnReqType(this.state.requestType)
let include = [
'application',
'association:id,name',
'unit:id,unit_number',
'status_history',
'status_history.user',
];
EservicesService.getEservicesRequestDetails(this.state.requestId, typr, include).then(response => {
console.log(response.record); // {prop1:'XXXXXXX',association:{name:'Association 1', ...}, ... }
console.log(response.record.association); // {name:'Association 1', ...}
this.setState({
error: response.error || null,
loading: false,
request: response.record
});
}).catch(error => {
this.setState({ error, loading: false });
});
}
Rendering Data渲染数据
render() {
console.log(this.state.request); // {prop1:'XXXXXXX',association:{name:'Association 1', ...}, ... }
console.log(this.state.request.association); // {name:'Association 1', ...}
return (
<View style={{ flex: 1 }}>
<ScrollView>
<Card >
<Text>Association: {this.state.request.association_id}</Text>
<Text>Unit: {this.state.request.association.name}</Text> {// Error Here "Cannot read 'name' of undefined"
}
</Card>
</ScrollView>
</View>
);}
Since fetch is an async call, So for initial render we wont get any data and hence it is throwing error.由于 fetch 是一个异步调用,所以对于初始渲染,我们不会获得任何数据,因此它会抛出错误。 So in order to avoid these exceptions we should check for the values before doing any operation.所以为了避免这些异常,我们应该在做任何操作之前检查这些值。
render() {
console.log(this.state.request); // {prop1:'XXXXXXX',association:{name:'Association 1', ...}, ... }
const {request} = this.state;
console.log(request.association); // {name:'Association 1', ...}
return (
<View style={{ flex: 1 }}>
<ScrollView>
<Card >
<Text>Association: {request.association_id && request.association_id}</Text>
<Text>Unit: {request.association.name && request.association.name}</Text> {// Error Here "Cannot read 'name' of undefined"
}
</Card>
</ScrollView>
</View>
);}
You need to check if the name is available, then render it.您需要检查名称是否可用,然后渲染它。
{ this.state.request.association && <Text>
Unit: {this.state.request.association.name && this.state.request.association.name}
</Text>
}
To prevent "falsy" render while fetching the data, you could simply put check for loading state in render function.为了在获取数据时防止“虚假”渲染,您可以简单地在渲染函数中检查加载状态。 Like this:像这样:
render() {
if (this.state.loading) {
return null
}
return <>Your fetched data</>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.