0

I have data in my console when I try to log it but I am not able to use it .

My data object looks like :

{_id: '616bf82d16a2951e53f10da4', name: 'abd', email: '[email protected]', phone: '1234567890', work: 'singer', …}

accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYxNmJmODJkMTZhMjk1MWU1M2YxMGRhNCIsImlhdCI6MTYzNDYyMDk3MiwiZXhwIjoxNjM1MDUyOTcyfQ.b9NZB-ogrdu_SulT1xJ8h62aHdyAo2jzTny2qakeaHY"

cpassword: "$2a$12$BZ17rY63qrq.Fw9wC29A.eabcuAHSY0mXfxSvcpxOFGfUeW4NUkMO"

email: "[email protected]"

name: "abd"

phone: "1234567890"

tokens: (3) [{…}, {…}, {…}]

work: "singer"

__v: 3

_id: "616bf82d16a2951e53f10da4"

[[Prototype]]: Object

import "./about.css"
import {useHistory} from "react-router-dom"


export default function About() {
    const history=useHistory();
    const [userData,setUserData]=useState();
    const callAboutPage=async()=>{
        try {
            const res=await fetch("/about",{
                method:"GET",
                headers:{
                    Accept:"application/json",
                    "Content-Type":"application/json"
                },
                credentials:"include"
            })
            const data=await res.json();
            console.log(data);
            setUserData(data);

            if(!res.status===200){
               const error=new Error (res.error)
               throw error
            }
            
        } catch (error) {
            console.log(error)
            history.push("/login")
        }
    }
    
  useEffect(() => {
     callAboutPage();
  }, [])

    return (
        <div className="abt-body">
            <div className=" d-flex justify-content-center align-items-center about">
                
                Name:{userData.name}
                <br />
                Id:{userData._id}
                <br />
                Email:{userData.email}
                <br />
                Phone:{userData.phone}
                <br />
                Work:{userData.work}
             
                

                

            </div>
           

                <div class="card-footer text-muted d-flex justify-content-center align-items-center">
    Designed and Developed by : Harsh Raj Ambastha
  </div>
        </div>
    )
}
1
  • 3
    Including personal information in a question that can be read by thousands of people is not good. Commented Oct 19, 2021 at 5:34

2 Answers 2

1

You might try using Javascript optional chaining: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

Try adding the operator to each of your fields:

userData?.name
userData?._id

etc.

Sign up to request clarification or add additional context in comments.

Comments

0

It is because you set initial value as empty which will be interpreted as primitive value. So when it renders initially it finds you are trying to access a property of a primitive value which ain't there. Give initial value as an empty object, it will then no longer throw an error here. useState({})

4 Comments

Thanks ,it worked for that error ,but still I am not able to render it on my dom. I tried to find out why this is happening ,when after setting the data I tried to log userData.name on console it is still showing undefined .Any solutions for it? Thanks in advance:)
Where are you using console.log(userData.name). If you can set this up in codesandbox, then provide the link. I can check it out
Changes which I made:const [userData,setUserData]=useState( {_id: "", name: "", email: "", phone: "", work: ""}); const data=await res.json(); setUserData(data);[HERE I DID LOG]console.log(userData.name)
useState() is like a await operation. It does not update your state immediately. so here is one thing to check that if you have value in console.log(data). If you have value in data, then you should try useState in separate value rather than in an object. Like const [name,setName], [email,setEmail] etc. Then set them separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.