Submit and clear button is throwing errors in my Mern stack application

I have been trying to submit my edited story and trying to clear the story by using a clear function but when i click on submit button after editing the story and when i click on a clear button to clear it throws me this error .
I am attaching an image of my errors and my complete updated code for reference.


You misspelled the function name. Look in the App component and compare that to what you have in the Form component.

@lasjorg Thanks for letting me know.

@lasjorg I am facing this issue after resolving .
image

1 Like

You need to push your code so we can see the updates.

My best guess would be you didn’t fix the function name in the component props so you are referencing a function that doesn’t exist in the component.

@lasjorg I have updated my code and now my clear function is working fine but my submit button in an editing option is not working yet neither it is throwing any error. I have pushed my updated code. Please check.

@bappyasif Please have a look into this. Thanks.

what is current situation?! is it still that same “seCurrentId” undefined error?

  • did you try to call this “setcurrentId” from “submit” function to see if same thing happens or not?
  • is this same function works properly from “Posts” component?
  • try to pass in “updaterFunction” instead of "hook update function"to child components to see if that problem resolves or not

happy coding :slight_smile:

@bappyasif Thank you for your response.

Currently, When I am trying to submit my editing story, it’s not working and it’s not showing any error.

Update :
I have resolved the ‘setCurrentId’ error.

  • elaborate on this, how is this not working, thanks :slight_smile:
  • well done :slight_smile:

@bappyasif When I try to submit my edited story, it’s not displaying the edited content and only shows the old one.
I am attaching a picture of my issue for reference:

Hope it makes sense to you.

  • are you using any “useEffect” hooks to “render” that data on page?! if so then you might wanna look into that, if not then please share that “code” which handles this functionality, thanks :slight_smile:

happy coding :slight_smile:

@bappyasif Here is my code which handles this functionality.

import React,{useEffect, useState} from 'react'
import useStyles from './styles';
import { TextField, Button, Typography,Paper } from '@material-ui/core';
import FileBase from 'react-file-base64';
//import { DeleteButton } from "@refinedev/mui";
//import { clear } from 'console';
import {useDispatch} from 'react-redux';
import { createPost ,updatePost} from '../../actions/posts.js';
//import { updatePost } from '../../../../server/controllers/posts';
import { useSelector } from 'react-redux';


 const  Form =({currentId, setCurrentId}) =>{
  const classes = useStyles();
  const [postData, setPostData] = useState({ creator: '', title: '', message: '', tags: '', selectedFile: '' });
  const post = useSelector((state) => (currentId ? state.posts.find((message) => message._id === currentId) : null));
  //const post = useSelector((state)=> currentId ? state.posts.find((p)=> p._id === currentId) :null);
  const dispatch = useDispatch();

  useEffect(()=> {
   if(post)  setPostData(post);
  },[post]);

  const handleSubmit =(e) =>{
    e.preventDefault();

    if(currentId){
      dispatch(updatePost(currentId, postData));
      //clear();
      
      
     
     
    }  else{
      dispatch(createPost(postData));
      //clear();
    }
    clear();
    
    
  }

  const clear =()=>{
    setCurrentId(null);
    setPostData({ creator: '', title: '', message: '', tags: '', selectedFile: '' });

  }
  return (
    
    <Paper className={classes.paper}>
      <form autoComplete='off' noValidate className={`${classes.root}${classes.form}`} onSubmit={handleSubmit}>
       <Typography variant='h6'> {currentId ? `Editing "${post.title}"` : 'Creating a Memory'}</Typography>
       <TextField name="creator" variant="outlined" label="Creator" fullWidth  value={postData.creator} onChange={(e) => setPostData({ ...postData, creator: e.target.value })} />
        <TextField name="title" variant="outlined" label="Title" fullWidth  value={postData.title} onChange={(e) => setPostData({ ...postData, title: e.target.value })} />
        <TextField name="message" variant="outlined" label="Message" fullWidth multiline rows={4}  value={postData.message} onChange={(e) => setPostData({ ...postData, message: e.target.value })} />
        <TextField name="tags" variant="outlined" label="Tags (coma separated)" fullWidth value={postData.tags} onChange={(e) => setPostData({ ...postData, tags: e.target.value.split(',') })} />
        <div className={classes.fileInput}>
        <FileBase type="file" 
        multiple={false} 
        onDone={({ base64 }) => setPostData({ ...postData, selectedFile: base64 })} 
        />
         <Button className={classes.buttonSubmit} variant="contained" color="primary" size="large" type="submit" fullWidth>Submit</Button>
         <Button variant="contained" color="secondary" size="small" onClick={clear} fullWidth>Clear</Button>
  

          </div>
      </form>

    </Paper>
  )
}

export default Form;
  • as i said, what value of “post” do you get when you are updating “setPostData”
  • does it even getting called when “submitting” your edit?
  • i suppose its getting served from “cached value”
  • consider looking into their docs and see if that helps

happy coding :slight_smile:

@bappyasif
I have tried to debug my code but unfortunately, I did not get my desired result.

Can you please help me out with this?

im trying to help you find that solution :slight_smile: did you look at those aforementioned points? if so what have you found and tried debugging, please consider sharing :slight_smile:

happy coding :slight_smile:

@bappyasif

  • Yes, I have tried those points and try to call setPostData when submitting my edited story and still my issue has not been resolved.

  • I also look into the documentation but was unable to fix my bug yet :frowning:

  • what value of “post” do you get when you are submitting it?
  • dont be discouraged its part of learning to overcome problems :slight_smile: look into other blogs, related docs :slight_smile:

@bappyasif

  • As I told you before, I am not getting any value of ‘Post’, when I am submitting it.
  • You can also check it by running it locally at your end.
  • then look into your usecase of “useSelector”

happy coding :slight_smile: