Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
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 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda