Skip to content
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

[BUG] Can't submit add post form without an image #37

Closed
alexkisielewicz opened this issue Jun 27, 2023 · 1 comment
Closed

[BUG] Can't submit add post form without an image #37

alexkisielewicz opened this issue Jun 27, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@alexkisielewicz
Copy link
Owner

The issue exists on the 'Add Post' page in the form, specifically regarding image file uploads. When a user attempts to submit the form without selecting an image, an error is returned stating, 'The submitted data was not a file. Please check the encoding type on the form.'

Expected behavior:
The user should be able to submit the form even without an image. In this case, the backend should provide a placeholder image.

It was checked: The encoding type is correctly set in the Axios defaults.

@alexkisielewicz alexkisielewicz added the bug Something isn't working label Jun 27, 2023
@alexkisielewicz alexkisielewicz self-assigned this Jun 27, 2023
@alexkisielewicz
Copy link
Owner Author

Upon investigation, I discovered that the issue in the code lies in PostCreateForm.js. Specifically, the problem arises from the following line:

formData.append("image", imageInput.current.files[0]);

The current files list is empty, yet it is still being appended to the formData.

The bug was resolved by conditionally appending the image to the formData only if the user selected an image. In this case, the file list is not empty and can be successfully appended to the formData.

Updated code snippet:

if (imageInput.current.files.length > 0) {
formData.append("image", imageInput.current.files[0]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: 🐛 Bugs
Development

No branches or pull requests

1 participant