-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updatePost component #12
Comments
formData is referring to data.posts[0] and formData._id should correctly refer to the _id of the Post Model Data , so passing it should correctly point to the post which we want to update. |
i did console log but still undefined so i used postId instead since it has the same value |
Using postID to replace may not solve the entire problem because the state of formData still has issues. When you console.log(JSON.stringify(formData)), you'll see three sets of data on the UpdatePost page. Let me explain why. The fact that you're seeing data in the console three times is due to the fetchPost() call inside useEffect(), which you invoke initially. This is triggered when the component is first called and when the postID changes. The changing postID is one reason useEffect() is called, and fetchPost() is called when the postID changes, causing the post data to be reloaded when there's a change. The first set, {}, is normal and empty because formData is initially set to an empty object ({}). The second set is the data loaded when the postID changes. This data shows all information, including _id, because it's set to formData when the post is initially loaded. The third set is similar to the second, but it doesn't include _id because you've updated setFormData to exclude _id in fetchPost(). You only want to update some parts of the post data. In this case, you might need to adjust how setFormData is set to ensure _id is included. You don't need to switch to using postID if you make changes to setFormData according to my suggestions. To fix it, update handleUploadImage as follows:
Use a state-setting function that takes prevFormData and returns the new combined data. This ensures all data in formData remains intact. Similarly, you should do the same with the remaining onChange for content and category.
I've given an example for one part only, so you'll need to make similar adjustments for content, category, etc. If you don't use prevFormData, you'll encounter another problem. This occurs when you fill in all fields but upload an image later. The image upload will reset the remaining formData state, preventing you from updating and showing an error for required inputs. Enjoy coding! |
in the client part of this project at the updatePost component inside the handleSubmit function
in the fetch url change the formData._id to postId because the formData._id is always undefined
The text was updated successfully, but these errors were encountered: