猿问

如何检查reactjs映射中的未定义

我正在开发一个应用程序,该应用程序使用它获取数据axios,然后将其存储在 redux 商店中。如果数据可以从 api 端点获得,那么一切正常,但是当我没有得到数据时就会出现问题,这给了我这样的错误。

类型错误:props.teamProduct._embedded 未定义

这就是我的数据在成功调用时的样子

当我得到未定义的结果时


http://img3.mukewang.com/62aa9bf10001620c10380259.jpg

这是我的代码


这就是我team从商店获取列表的方式


const mapStateToProps = (state) => {

  return {

    

    team: state.productDetails.team.team.data,

  };

};

之后我将它传递给组件


产品明细


{teamProduct &&

              teamProduct._embedded.cartResourceList.map((item, index) => {

                console.log("item", item);

                if (index < 2) {

                  return (

                    <Grid

                      key={index}

                      container

                      item

                      xs={12}

                      className="mid_box"

                    >

                      <Grid container item xs={6}>

                      

                      </Grid>

                    </Grid>

                  );

                }

              })}

有没有办法在这里检查未定义的值。


狐的传说
浏览 116回答 1
1回答

繁星点点滴滴

需要检查您实际需要的所有属性访问权限{teamProduct &&&nbsp;&nbsp; teamProduct._embedded &&&nbsp; teamProduct._embedded.cartResourceList &&&nbsp; teamProduct._embedded.cartResourceList.map((item, index) => {&nbsp; &nbsp; console.log("item", item);&nbsp; &nbsp; if (index < 2) {&nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; <Grid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key={index}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xs={12}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; className="mid_box"&nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Grid container item xs={6}></Grid>&nbsp; &nbsp; &nbsp; &nbsp; </Grid>&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; })}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答