简体   繁体   English

如何显示元素名字和姓氏...来自 strore redux

[英]how to display element firstname and lastname ... from strore redux

find here my code whene I try to get firstName I get the issue “Cannot read properties of undefined” could some one give a solution or expalain what's the issue whith my code在这里找到我的代码,当我尝试获取名字时,我得到了“无法读取未定义的属性”的问题,有人可以提供解决方案或解释我的代码有什么问题吗

let state = useSelector(state => {
      // console.log('State: ', state.profile);
       return state.profile;
     });


   const [profile, setProfile] = useState();
   const [formState, setFormState] = useState({
       isValid: false,
       values: {},
       touched: {},
       errors: {}
   });

   useEffect(()=>{
       console.log({accesstoken: token});
   if(token)
       dispatch(profileActions.getInfos({accesstoken: token}));
   },[])


   useEffect(()=>{
       if (state)
       setProfile(state)
       console.log(profile.profile);
   },[state, profile])
/// here the peace of code to display the profile.data.firstname
ListItemText primary="Date de creation" secondary{JSON.stringify(profile.data.firstName)} />

在调试中,我可以在 usestate 中使用选择器获取状态

Did you console the variable state just before your useEffect.您是否在 useEffect 之前对变量 state 进行了控制台操作。 I guess no need to use (useState & useEffect) you can directly use yout variable state.我想不需要使用(useState & useEffect)你可以直接使用你的变量 state。 try to add a console.log(state) before or after the useEffect and share it with as.尝试在 useEffect 之前或之后添加 console.log(state) 并与 as 共享。

I try我试试

const profile = useSelector(state => state.profile.profile.data);
console.log(profile);



output => {
"_id": "61d6cbe4e3afe444b1500f50",
"email": "fridmahdi@live.fr",
"firstName": "elmahdi",
"lastName": "frid",
"number": null,
"created_at": "2022-01-06T11:00:52.009Z",
"is_verified": null}

const profile = useSelector(state =>state.profile.profile.data.firstName) 第一次调试不带名字 字段错误

but when i add the field that I need like this i get undifined但是当我像这样添加我需要的字段时,我变得不明确

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

相关问题 如何从 node.js 中的 email 提供商获取名字和姓氏? - How to get firstname and lastname from email provider in node.js? 在passportjs中处理名字和姓氏 - Handling firstname and lastname in passportjs 查询名字,姓氏为全名 - Query firstname, lastname as fullname 给我的_id加上名字和姓氏 - Gendered my _id with firstname and lastname 从Node.js中MongoDB中持久保存的全名中获取名字和姓氏 - Get firstName and lastName from Full Name persisted in MongoDB in Node.js mongoDB 中 concat firstName 和 lastName 中不区分大小写的搜索 - Case insensitive search in concat firstName and lastName in mongoDB 在邮递员上运行时如何请求数据。 目前,电子邮件,名字和姓氏已硬编码到代码中。 如何索取数据? - How to request for data when I run on postman. At the moment the email, firstname and lastname are hardcoded to the code. How do I request the data? Mongoose:findByIdAndUpdate:尝试更新所有带有名称字段(firstName + lastName)的文档 - Mongoose: findByIdAndUpdate: Trying to update all documents with name field (firstName + lastName) Node.js表示JWT如何从Web /电话客户端(jsonwebtoken)发出请求并发送令牌 - Nodejs express JWT how to strore and send token from web/phone client (jsonwebtoken) 在玉石中如何解释? &lt;%=姓氏%&gt; - In Jade how is this interpreted ? <%= lastName %>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM