Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Cannot read properties of null (reading '_id')

so i have 2 buttons one delete and an edit, i only want the one who commented to be able to delete and edit his post, the delete button is working but i didn't test the edit one because i kept on getting error, My problem is it was working before, only the authorized and the one who commented could view the post, but i went back to do some css and now keep on getting "Cannot read properties of null (reading '_id')"

function PostItem({el}) {
     const dispatch = useDispatch()
     const history= useHistory()
     const [text, setText]=useState("")
     const auth =  useSelector(state=> state.UserReducer.auth)
     const handleSubmit=(e)=>{
      e.preventDefault();
      dispatch(AddComment(el._id, {text}));
    
    
     };


     useEffect(() => {
  //  dispatch(Getpost(el._id))
  dispatch(current())
     }, [])
     const reducerUser = useSelector(state => state.UserReducer.user)
    

    return(
        
      <section className="dark">
      <div className="container-p">
       
        <article className="newPost">
         
          <div className="post-head">
            <h6 className="post-name">{el.name}</h6>
            <div className="post-body">
              <time dateTime="date-post">
                 <i className="date-post" /><Moment>"YYYY/MM/DD" </Moment> 
              </time>
            </div>
            <div className="post-text" />
            <p>{el.text}</p>
            
            {auth  &&  reducerUser._id == el.user &&
<div>
             <button onClick={()=>{dispatch(DeletePost(el._id))}}>
               Delete Post
             </button> 
             <button onClick={()=>{dispatch(EditPost(el._id))}}>EDIT
             </button>
              </div>
            }
            
            
            </div>
            <form onSubmit={handleSubmit}>
              <textarea   value={text} onChange={(e)=>setText(e.target.value)}   rows="2" cols="100" >
              </textarea>
              
              <button className='btn' type="submit" > add comment</button>
              <p>
                {el.comments.map(el=> el.text && el.text )}
              </p>
            </form>
        </article>
      </div></section>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try to use Null Propagation operator changing the reducerUser._id for reducerUser?._id.

about 4 years ago · Juan Pablo Isaza Report

0

You can use Optional chaining in your el object on each button component to prevent the error and inside your store methods validate if the value is null just ignore it like this:

function PostItem({el}) {
     const dispatch = useDispatch()
     const history= useHistory()
     const [text, setText]=useState("")
     const auth =  useSelector(state=> state.UserReducer.auth)
     const handleSubmit=(e)=>{
      e.preventDefault();
      dispatch(AddComment(el._id, {text}));
    
    
     };


     useEffect(() => {
  //  dispatch(Getpost(el._id))
  dispatch(current())
     }, [])
     const reducerUser = useSelector(state => state.UserReducer.user)
    

    return(
        
      <section className="dark">
      <div className="container-p">
       
        <article className="newPost">
         
          <div className="post-head">
            <h6 className="post-name">{el.name}</h6>
            <div className="post-body">
              <time dateTime="date-post">
                 <i className="date-post" /><Moment>"YYYY/MM/DD" </Moment> 
              </time>
            </div>
            <div className="post-text" />
            <p>{el.text}</p>
            
            {auth  &&  reducerUser?._id == el.user &&
<div>
             <button onClick={()=>{dispatch(DeletePost(el?._id))}}>
               Delete Post
             </button> 
             <button onClick={()=>{dispatch(EditPost(el?._id))}}>EDIT
             </button>
              </div>
            }
            
            
            </div>
            <form onSubmit={handleSubmit}>
              <textarea   value={text} onChange={(e)=>setText(e.target.value)}   rows="2" cols="100" >
              </textarea>
              
              <button className='btn' type="submit" > add comment</button>
              <p>
                {el.comments.map(el=> el.text && el.text )}
              </p>
            </form>
        </article>
      </div></section>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!