简体   繁体   English

FourSquare Axios请求-ReactJs-无法读取未定义的属性“ map”

[英]FourSquare Axios Request - ReactJs - Cannot read property 'map' of undefined

please help :) 请帮忙 :)

I'm doing simple Axios get request, in a "Component Did Mount". 我在“ Component Did Mount”中执行简单的Axios获取请求。 In a simple/basic react app. 在简单/基本的React应用中。 I'm trying to get FourSquare place data for London. 我正在尝试获取伦敦的FourSquare地点数据。

My function works, as I can console.log data. 我的功能正常,因为我可以console.log数据。 Here's my component did mount code: 这是我的组件安装代码:

// FOURSQUARE COMPONENT DID MOUNT
componentDidMount() {
  console.log('COMPONENT DID MOUNT');
  axios.get('https://api.foursquare.com/v2/venues/explore…')

  .then(res => {
   console.log('data', res.data.response.groups[0].items);
// console.log('data',res.data.response.groups[0].items[0].venue.name);
   this.setState(
   { places: res.data.response.groups[0].items}
  );
 });
}

Please here the screenshot attached of console.log. 请在此处附上console.log的屏幕截图。 https://i.imgur.com/DPPxwdW.png https://i.imgur.com/DPPxwdW.png

However, I'm trying to map through the data, to get "venue.name" in the object array/res.data. 但是,我正在尝试映射数据,以在对象array / res.data中获取“ venue.name”。 But I keep getting the following error: 但是我一直收到以下错误:

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

Here is how I'm trying to map through the data.... 这是我试图通过数据映射的方式。

const Loaders = ({places}) => {

  return (

    <section>
      <div className="columns is-multiline">
        {places.map((place, i) => <div className="column is-one-quarter" key={i}>
          <ul>
            <li>
              <div className="card-image">
                <figure className="image">
                 <h2 className="has-text-centered has-text-grey">title: {place.venue.name}</h2>
                </figure>
              </div>
            </li>
          </ul>
        </div>)}
      </div>
    </section>
  );
};

export default Loaders;

The reason that happens because it the request is asynchronous and after the response is obtained then the places state is set. 发生这种情况的原因是因为该请求是异步的,并且在获得响应之后便设置了places状态。 So initially, places is undefined. 因此,最初, places是不确定的。 You can render a loading indicator in the loading state 您可以在加载状态下呈现加载指示器

const Loaders = ({places}) => { if(!places){ return Loading... } const Loaders =({{places})=> {if(!places){return Loading ...}

return ( 返回(

<section>
  <div className="columns is-multiline">
    {places.map((place, i) => <div className="column is-one-quarter" key={i}>
      <ul>
        <li>
          <div className="card-image">
            <figure className="image">
             <h2 className="has-text-centered has-text-grey">title: {place.venue.name}</h2>
            </figure>
          </div>
        </li>
      </ul>
    </div>)}
  </div>
</section>

); ); }; };

or initialize places to empty array in the constructor 或在构造函数中将位置初始化为空数组

constructor(props) {
   this.state = {
     places = []
   }
}

The problem is that this.state.places is undefined at some point. 问题是this.state.places在某些时候是未定义的。 The problem could be the initial state not being set to an empty array at the start or something in the logic changing the value of places . 问题可能是初始状态在开始时未设置为空数组,或者逻辑中的某些值更改了places的值。 Try to 尝试

class Foo extends React.Component {
    state = {
        places: []
    }

    render() {
        return <Loaders places={this.state.places} />>
    }
}

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

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