Cannot read property 'map' of undefined

Posted taxun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cannot read property 'map' of undefined相关的知识,希望对你有一定的参考价值。

报错:Cannot read property ‘map‘ of undefine
  <ul className={styles.resul}>
            {
              this.state.isfalsepic.map((item) => {
                return (
                  <li className={styles.resli} key={item.id}>
                    <img
                      className={styles.resimg}
                      alt="example"
                      src={`${this.state.imgpath}media/${item.user_info_price}`}
                    />
                  </li>
                );
              })}
          </ul>

 

原因:调用map的对象是 undefined,初始化第一次渲染的时候异步数据返回之前list是undefined。


解决方法:对list作判断,异步ajax返回数据list取到值后再渲染组件。

 <ul className={styles.resul}>
            {this.state.isfalsepic &&
              this.state.isfalsepic.map((item) => {
                return (
                  <li className={styles.resli} key={item.id}>
                    <img
                      className={styles.resimg}
                      alt="example"
                      src={`${this.state.imgpath}media/${item.user_info_price}`}
                    />
                  </li>
                );
              })}
          </ul>

  

以上是关于Cannot read property 'map' of undefined的主要内容,如果未能解决你的问题,请参考以下文章

Uncaught TypeError: Cannot read property 'addEventListener'

Uncaught TypeError: Cannot read properties of undefined (reading 'getUserMedia')

axios报错: Cannot read property 'protocol' of undefined ....

解决报错Cannot read property 'forceUpdate' of undefined

VUE TypeError: Cannot read property 'validate' of undefined

Cannot read property 'map' of undefined