简体   繁体   English

当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性'名称'”错误

[英]React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details

I want to show my users details in modal when I click on my users list. 当我单击用户列表时,我想以模式显示用户详细信息。 I pass the user ID as props to modal and in modal I find the user by javascript find() method and user_id. 我将用户ID作为道具传递给模态,并在模态中通过javascript find()方法和user_id找到用户。 this method returns my requested user as an Object and I can show it by console.log() but when I want to access to object properties for example "user.name" or "user.email" react thrown an error: TypeError: Cannot read property 'name' of undefined 此方法以对象的形式返回我请求的用户,我可以通过console.log()进行显示,但是当我要访问对象属性(例如“ user.name”或“ user.email”)时,会引发错误:TypeError:无法读取未定义的属性“名称”

//users.js code part

{ showUserDetails ? <div className="ModalContainer" >
       <div className="userModal">
           <div className="modalBody">
               <span className="close">X</span>
               <SelectedUser selectedUserId={selectedUserId}/>
           </div>
       </div>
   </div> : <div></div>
}
// SelectedUser.js codes

import React from 'react';
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";

const SelectedUser = ({selectedUserId, users}) => {
    let user = users.find(user=> user.id === selectedUserId)
    console.log(user.name)
    return ( 
       <React.Fragment>
            <p><b>Name:</b> {user.name}</p>
            <p><b>User Name:</b> {user.username}</p>
            <p><b>Email:</b> {user.email}</p>
            <p>
                <b>Address:</b> 
                {`  ${user.address.suite}, ${user.address.street}, ${user.address.city}
                [zip Code: ${user.address.zipcode}]
                `}
            </p>
            <p><b>Phone: </b>{user.phone}</p>
            <p><b>WebSite: </b>{user.website}</p>
            <p><b>Company: </b>{user.company.name}</p>
       </React.Fragment>
     );
}

const mapStateToProps = state => ({
    users: Object.values(state.users.users)
})


export default withRouter(connect(mapStateToProps)(SelectedUser));

In your SelectedUser component you should check if the users-state is available before rendering. 在您的SelectedUser组件中,应该在呈现之前检查用户状态是否可用。

import React from 'react';
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";

const SelectedUser = ({selectedUserId, users}) => {
    let user;
    let content;
    if(users && users.length > 0){
        user = users.find(user=> user.id === selectedUserId);
    } else {
        return <div>Loading....</div>
    }

    if(Object.keys(user).length >= 0){
       content = (<React.Fragment>
            <p><b>Name:</b> {user.name}</p>
            <p><b>User Name:</b> {user.username}</p>
            <p><b>Email:</b> {user.email}</p>
            <p>
                <b>Address:</b> 
                {`  ${user.address.suite}, ${user.address.street}, ${user.address.city}
                [zip Code: ${user.address.zipcode}]
                `}
            </p>
            <p><b>Phone: </b>{user.phone}</p>
            <p><b>WebSite: </b>{user.website}</p>
            <p><b>Company: </b>{user.company.name}</p>
       </React.Fragment>)
   } else {
       content = "Loading"
   }
   return(
      <div>{content}</div>
   )
}

const mapStateToProps = state => ({
    users: Object.values(state.users.users)
})


export default withRouter(connect(mapStateToProps)(SelectedUser));

暂无
暂无

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

相关问题 TypeError:无法读取 React 中未定义的属性“多对象中的状态名称” - TypeError: Cannot read property 'state name in much object' of undefined in React 类型错误:无法读取未定义反应错误的属性“名称” - TypeError: Cannot read property 'name' of undefined react error 反应错误类型错误:无法读取未定义的属性“名称” - React Error TypeError: Cannot read property 'name' of undefined React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined 反应:类型错误:无法读取未定义的属性“名称” - REACT: TypeError: Cannot read property 'name' of undefined TypeError:在 useState 中传递 object 时,无法读取未定义的属性“名称” - TypeError: Cannot read property 'name"' of undefined in react when passing object in useState 当我想获取数据时出现此错误:TypeError:无法读取未定义的属性“数据” - When i want to fetch data i have this error : TypeError: Cannot read property 'data' of undefined TypeError:无法读取react js中未定义的属性“then” - TypeError: Cannot read property 'then' of undefined in react js “ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - “TypeError: Cannot read property 'name' of undefined” error 错误类型错误:当我尝试添加产品时无法读取未定义的属性“名称” - ERROR TypeError: Cannot read property 'name' of undefined when i try to add product
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM