0

frontend code:

{

(opp||[]).map(opps=>{
    return(
        <div className="card OppCard">
        <div className="card-body" >
            <h4>{opps.title}</h4>
            <p><b>Author : {opps.postedBy.name}</b></p>
            <p style={{ "margin": "0px" }}>Description : {opps.desc}</p>
            <p style={{ "margin": "0px" }}>Location : {opps.location}</p>
            <p style={{ "margin": "0px" }}>Experience Required : {opps.experience}</p>
            <p style={{ "margin": "0px" }}>Last Date to Apply : {opps.ld}</p>
        </div>
    </div>
    )
}) 
}

At <p><b>Author : {opps.postedBy.name}</b></p> this line error occurs here is a pic attached for opps object: opps is the object and postedBy is also an object, how should I access opps.postedBy.name????

3
  • 1
    you do opps.postedBy && opps.postedBy.name this will work Commented Nov 17, 2021 at 5:53
  • @AshishKamble its undefined Commented Nov 17, 2021 at 5:53
  • @AshishKamble thnx bro it worked, can u explain it though Commented Nov 17, 2021 at 5:55

1 Answer 1

1

whenever you try to access json object, check if it is undefined or not beffore calling method or accessing properrty replace this line,

<p><b>Author : {opps.postedBy.name}</b></p>

with this, instead, this will work because you are checking before accessing it, also when you try to .filter() or .map() first check if it is empty or not, if not empty then only call, else it will throw error

<p><b>Author : {opps.postedBy && opps.postedBy.name}</b></p>
Sign up to request clarification or add additional context in comments.

Comments

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.