简体   繁体   English

访问 object 中的数组长度给我“TypeError:无法读取未定义的属性“长度”

[英]accessing length of an array inside an object gives me 'TypeError: Cannot read property 'length' of undefined'

I get an error TypeError: Cannot read property 'length' of undefined when trying to access length of an array inside of an object that I get from mapstatetoprops... In my actions folder, i created a function getuserdata that makes an api call and successfully returns an object.我收到一个错误TypeError: Cannot read property 'length' of undefined当试图访问我从 mapstatetoprops 获得的 object 中的数组长度时...成功返回 object。 in my component i call this.props.getuserdata in componentdidMount, and i mapstatetoprops currentUser state with getUserdata... although tt returns an object, it does not allow me to access the length of one of the properties arrays.... my mapstatetoprops writes: in my component i call this.props.getuserdata in componentdidMount, and i mapstatetoprops currentUser state with getUserdata... although tt returns an object, it does not allow me to access the length of one of the properties arrays.... my mapstatetoprops写道:

currentUser: state.currentUser.getUserData当前用户:state.currentUser.getUserData

components folder组件文件夹


    class Users extends Component {

  componentDidMount() {
    this.props.getUserData();
    this.props.getOtherUsers();
   }

  render() {
      console.log(this.props.currentUser.myStocks.length,'current user')
      
      return (
      <Container>
      
      </Container>
    )
  }
};
function mapStateToProps(state) {
  return {
   currentUser: state.currentUser.getUserData, 
    filteredUsers: state.filteredUsers.getOtherUsers,
    getUsersError: state.users.getUsersError
  };
}

export default compose(
  connect(mapStateToProps, { getUserData, getOtherUsers }),
  requireAuth
)(Users);

actions folder:动作文件夹:


    export const getUserData = () => async (dispatch) => {
   try {
    const { data } = await axios.get('/api/user/profile', {
      headers: { authorization: localStorage.getItem('token') },
    });
    // console.log(data.myStocks[0],'inform')
    dispatch({ type: GET_USER_DATA, payload: data });
  } catch (e) {
    dispatch({
      type: GET_USER_DATA_ERROR,
      serverError: e,
      clientError: 'Something went wrong please refresh try again',
    });
  }
}

If this.props.currentUser is supposed to be obtained by this.props.getUserData() maybe the problem is that you API call is not finish while react use the render method.如果this.props.currentUser应该通过this.props.getUserData()获得,则问题可能是您的 API 调用未完成,而反应使用render方法。

暂无
暂无

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

相关问题 访问 cart.order_items.length 给出 TypeError: Cannot read property 'length' of undefined - Accessing cart.order_items.length gives TypeError: Cannot read property 'length' of undefined 未被捕获的TypeError:通过Json Object访问arraylist时无法读取未定义的属性&#39;length&#39; - Uncaught TypeError: Cannot read property 'length' of undefined while accessing arraylist through Json Object 在函数内部 &gt;&gt;&gt; TypeError:无法读取未定义的属性“长度” - Inside the function >>> TypeError: Cannot read property 'length' of undefined 错误类型错误:无法读取 Object.updateDirectives 处未定义的属性“长度” - ERROR TypeError: Cannot read property 'length' of undefined at Object.updateDirectives 数组迭代的“未捕获的类型错误:无法读取未定义的属性‘长度’” - "Uncaught TypeError: Cannot read property 'length' of undefined" for an array iteration 未捕获的TypeError:无法读取未定义的属性“长度”(数组) - Uncaught TypeError: Cannot read property 'length' of undefined (array) TypeError:无法在反应中读取未定义的数组随机播放的属性“长度” - TypeError: Cannot read property 'length' of undefined array shuffle in react 未捕获的类型错误:无法读取简单 JSON 数组的未定义属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined for simple JSON array Javascript Uncaught TypeError:无法读取未定义的属性“ length”(使用数组) - Javascript Uncaught TypeError: Cannot read property 'length' of undefined (using an array) 未捕获的类型错误:无法读取未定义数组的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM