为什么会发生TypeError:无法读取未定义的“地图”属性?

时间:2020-09-07 12:22:20

标签: reactjs dictionary

contanier.js

class PatientListContainerNew extends Component {
  
  state = {
    limit: 7,
    file: {
      info: null,
      base64: null,
    },
    data: [],
    filteredData: [],
    columns: [],
    searchInput: "",
  };

  componentDidMount() {
    this.getData();
    this.getColumns();
    console.log("========componentDidMount====== ");
    const { getPatient, loadingTrue } = this.props;
    loadingTrue();
    setTimeout(() => {
      getPatient();
    }, 500);
  }
  

  ... code skip


getData = () => {
  const originData = this.props.results;
  const data = this.props.results.map((datum) => ({...datum, id: datum.id, name: datum.name}));
  this.setState({ data, filteredData: data });
};

handleSetData = data => {
  console.log(data);
  this.setState({ filteredData: data });
};


  render() {
  const { filteredData, columns } = this.state;
    return (
      <div>
        <PatientSearch
          data={this.state.data}
          handleSetData={this.handleSetData}
        />
        <List
          columns={columns}
          originData={filteredData}
          limit={this.state.limit}
        />
        <input />
      </div>
    );
  }
}

const mapStateToProps = state => ({
  results: state.patient.results,
});

错误代码

TypeError: Cannot read property 'map' of undefined
PatientListContainerNew.getData
D:/src/containers/patient/Container.js:346
  343 | 
  344 |    getData = () => {
  345 |      const originData = this.props.results;
> 346 |      const data = originData.map((datum) => ({...datum, id: datum.pid, name: datum.pid}));
      | ^  347 |      this.setState({ data, filteredData: data });
  348 |    };
  349 | 

origindata保存数据库中的数据。

想知道为什么出现地图错误。 我对如何使用地图功能感到困惑。我想我用错了。如果您让我们知道如何正确使用它,我们将不胜感激。

如有必要,我将添加更多代码。

感谢所有回答的人。

添加照片

enter image description here

2 个答案:

答案 0 :(得分:0)

您可能没有获得期望的数据。解决此问题的一种方法是使用构造函数提供状态为空的数据集:

class PatientListContainerNew extends Component {
  constructor(props) {
    super(props);
    this.state = {
      originData = this.props.results || [];
      ...
    }
  }
}

然后您就可以在this.state.originData上工作,而不会发生错误。

答案 1 :(得分:0)

请检查是否在“ this.props.results”中获得了正确的数据 我认为您没有获得正确的数据或使用地图与财产无关。