简体   繁体   English

反应“未捕获的TypeError:无法读取未定义的属性”

[英]React “Uncaught TypeError: Cannot read property of undefined”

I'm React newbie and struggling. 我是新手,正在挣扎。 Following snippet gives the following error 以下代码段显示以下错误

"Uncaught TypeError: Cannot read property 'creationDate' of undefined". “未捕获的TypeError:无法读取未定义的属性'creationDate'”。

If I move code from populateTableRows and creationDate functions inside render, everything works nicely. 如果我从render内部的populateTableRows和creationDate函数中移动代码,那么一切都会很好地工作。 SurveyList gets it's data from another component. SurveyList从另一个组件获取数据。 I know this is pretty ugly component and all other suggestions are welcome too, but I'm mostly interested in this one particular error. 我知道这是一个非常丑陋的组件,所有其他建议也都受到欢迎,但是我对这一特定错误最感兴趣。

import React from 'react';
import ReactDOM from 'react-dom';
import { Table, Tr, Td } from 'reactable';

class SurveyList extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      isSaved: false,
      surveys: []
    };
    this.creationDate = this.creationDate.bind(this);
    this.populateTableRows = this.populateTableRows.bind(this);    
  }

  creationDate (obj){
    return new Date(obj._id.time).toISOString().slice(0,10);
  }

  populateTableRows(surveys){
    var surveyRows = [];
    surveys.forEach(function(obj){
      surveyRows.push(
        <Tr key={obj.surveyId}>
          <Td column="SurveyId">{obj.surveyId}</Td>
          <Td column="Survey name">{obj.surveyName}</Td>
          <Td column="Creation date">{this.creationDate(obj)}</Td>
          <Td column=""><ModalDialog key={obj.surveyId}
                                     survey={obj}
          /></Td>
        </Tr>
      );
    });
    return surveyRows;
  }

  render() {
    var surveys = Array.from(this.props.surveys);
    var surveyRows = this.populateTableRows(surveys);
    return (
      <Table className="table" id="table" sortable={true} filterable={['SurveyId', 'Survey name', 'Creation date']}>
        {surveyRows}
      </Table>
    )
  }
}

The comment by @ctrlplusb is correct. @ctrlplusb的注释正确。 When you use a function keyword as you have done in your surveys.forEach call, its contents get a new scope - and therefore a new this , which is undefined as it belongs to no object. 当您像在surveys.forEach调用中一样使用function关键字时,其内容将获得新的作用域-因此也将获得新的this ,由于它不属于任何对象,因此未定义。 There are several solutions. 有几种解决方案。

The prettiest is to use the new fat arrow ("lexical this ") syntax available in ES2015 through Babel. 最漂亮的是使用通过Babel在ES2015中可用的新的胖箭头(“ lexical this ”)语法。 It creates a function that maintains the scope in which it was defined. 它创建一个函数来维护其定义范围。 Eg: 例如:

surveys.forEach( obj => surveyRows.push(/* ... */) );

However, the simplest is to use the second argument that forEach takes , which is the this to use: 然而,最简单的就是使用的是第二个参数forEach需要 ,这是this使用方法:

surveys.forEach( function ( obj ) {
  surveyRows.push(/* ... */);
}, this );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应“未捕获的TypeError:无法读取未定义的属性” - React “Uncaught TypeError: Cannot read property of undefined” 反应-未捕获的TypeError:无法读取未定义的属性“ 1” - React - Uncaught TypeError: Cannot read property '1' of undefined 反应未捕获的TypeError:无法读取未定义的属性[property_name] - React Uncaught TypeError: Cannot read property [property_name] of undefined 反应:未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - React: Uncaught TypeError: Cannot read property 'setState' of undefined React Apollo:未捕获(承诺)类型错误:无法读取未定义的属性“重新获取” - React Apollo: Uncaught (in promise) TypeError: Cannot read property 'refetch' of undefined 未捕获的类型错误:无法读取未定义的属性“映射”(REACT NATIVE - Javascript) - Uncaught TypeError: Cannot read property 'map' of undefined ( REACT NATIVE - Javascript ) 未捕获的TypeError:无法在react中读取未定义的属性&#39;map&#39; - Uncaught TypeError: Cannot read property 'map' of undefined in react 未捕获的TypeError:无法使用React和Laravel读取未定义的属性&#39;func&#39; - Uncaught TypeError: Cannot read property 'func' of undefined using React and Laravel React:未捕获的TypeError:无法读取未定义的属性&#39;set&#39; - React:Uncaught TypeError: Cannot read property 'set' of undefined 反应引导-未捕获的TypeError:无法读取未定义的属性&#39;toUpperCase&#39; - React bootstrap - Uncaught TypeError: Cannot read property 'toUpperCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM