简体   繁体   English

无法读取未定义REACT的属性“地图”

[英]Cannot read property 'map' of undefined REACT

constructor(){
    super();
    this.state = {
        lista: [],
    }
}      

componentDidMount(){
    fetch('http://lospraianos-env-1.qu3skpxsmw.sa-east-1.elasticbeanstalk.com/api/materials')
      .then(response => response.json())
      .then(resData => {
        this.setState( {data: resData.results});
      })

}

<div>
  <table className="pure-table">
    <thead>
      <tr>
        <th>id</th>
        <th>Produto</th>
        <th>Quantidade</th>
      </tr>
      </thead>
        <tbody>{
          this.props.lista.map(function(data){
            return (  
              <tr key={data.codMat}>
                <td>{data.codMat}</td>
                <td>{data.material}</td>
                <td>{data.qntMin}</td>
              </tr>
              );
            })
            }
        </tbody>
  </table>
</div>

I'm trying to get info from the API and make a table with it but I'm getting some errors. 我正在尝试从API获取信息并使用它制作表格,但出现了一些错误。

Cannot read property 'map' of undefined 无法读取未定义的属性“地图”

how can I deal with it? 我该如何处理?

I'd recommend doing a conditional rendering here, by checking lista property first, before mapping all data. 我建议在这里进行条件渲染,方法是在映射所有数据之前先检查lista属性。

{
          this.props.lista && this.props.lista.map(function(data){
            return (  
              <tr key={data.codMat}>
                <td>{data.codMat}</td>
                <td>{data.material}</td>
                <td>{data.qntMin}</td>
              </tr>
              );
            })
            }

As well as you have a error in 以及您在

this.setState( {data: resData.results});

As you need to set state for lista:resData.results , not data 由于您需要为lista:resData.results设置状态,而不是data

You're setting this. 您正在设置。 state .lista to contain array data. state .lista包含数组数据。

You're trying to map over this. 您正在尝试对此进行映射。 props .lista. props .lista。

this.state !== this.props. this.state!== this.props。

Change this.props.lista.map to this.state.lista.map and it should work. 更改this.props.lista.mapthis.state.lista.map ,它应该工作。

There are several problems with your codes. 您的代码有几个问题。 Try to fix these: 尝试解决这些问题:

 constructor(props) {  //fixed
    super(props);  //fixed
    this.state = {
      lista: []
    };
  }

  componentDidMount() {
    fetch(
      'http://lospraianos-env-1.qu3skpxsmw.sa-east-1.elasticbeanstalk.com/api/materials'
    )
      .then(response => response.json())
      .then(resData => {
        this.setState({ lista: resData.results });   //fixed
      });
  }

and... 和...

  {
    this.state.lista.length > 0 && this.state.lista.map(function(data) {   //fixed
      return (
        <tr key={data.codMat}>
          <td>{data.codMat}</td>
          <td>{data.material}</td>
          <td>{data.qntMin}</td>
        </tr>
      );
    })
  }

You are setting response to a state variable data which doesn't exist in your code. 您正在设置对代码中不存在的状态变量数据的响应。 You need to set it to lista instead of data in fetch Api call like 您需要将其设置为lista而不是在获取Api调用中的数据,例如

    this.setState({lista : resData.results});

And here do conditional check and do .map 在这里进行条件检查并执行.map

.map without return .map不返回

  <tbody>{
      this.props.lista && this.props.lista.map(data => (
          <tr key={data.codMat}>
            <td>{data.codMat}</td>
            <td>{data.material}</td>
            <td>{data.qntMin}</td>
          </tr>
        ))
        }
    </tbody>

.map with return .map与返回

  <tbody>{
      this.props.lista && this.props.lista.map(data => {
          return (<tr key={data.codMat}>
            <td>{data.codMat}</td>
            <td>{data.material}</td>
            <td>{data.qntMin}</td>
          </tr>)
        })
        }
    </tbody>

I see that you've set the initial state with that array lista[] . 我看到您已使用该数组lista[]设置了初始状态。 Do you have one array like that passed as a prop as well? 您是否也有一个像道具那样传递的数组? If yes check for typos and you might also want to use the constructor with the props argument that you will pass in the super call like so 如果是,请检查拼写错误,您可能还希望将构造函数与props参数一起使用,这样您将在超级调用中进行传递

constructor(props){
     super(props);
     .......
}

As far as i can see, there are 2 problems with the given code, 据我所知,给定的代码有2个问题,

the first one: 第一个:

You are running the Array.prototype.map() on this.props.lista , when the actual array is this.state.lista 当实际数组是this.state.lista时,您正在this.props.lista上运行Array.prototype.map()

Also, componentDidMount() runs after the render() function, so either move the fetch() to the constructor or set a condition for you map function 另外, componentDidMount()render()函数之后运行,因此将fetch()移至constructor或为map函数设置条件

Read https://reactjs.org/docs/react-component.html#mounting for better understanding of React.component's lifecycles 阅读https://reactjs.org/docs/react-component.html#mounting以更好地了解React.component的生命周期

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM