简体   繁体   English

React Native:错误:“无法读取未定义的属性‘名称’,”虽然包含名称的对象不是未定义的

[英]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.

相关问题 React Native TypeError:无法读取未定义的属性“名称” - React Native TypeError: Cannot read property 'name' of undefined Javascript:原生JavaScript代码出现“无法读取未定义的属性”名称”错误 - Javascript: “Cannot read property ”name“ of undefined” error at native javascript code TypeError:无法读取 React 中未定义的属性“多对象中的状态名称” - TypeError: Cannot read property 'state name in much object' of undefined in React 无法读取未定义的属性“名称” - 反应 - Cannot read property 'name' of undefined - react 反应:类型错误:无法读取未定义的属性“名称” - REACT: TypeError: Cannot read property 'name' of undefined 类型错误:无法读取未定义反应错误的属性“名称” - TypeError: Cannot read property 'name' of undefined react error 反应错误类型错误:无法读取未定义的属性“名称” - React Error TypeError: Cannot read property 'name' of undefined React Native无法读取属性未定义错误 - React Native cannot read property undefined error 当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details 错误:“无法读取未定义的属性‘名称’” - error: “Cannot read property 'name' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM