question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using multipart/form-data with multer occasionally causes React app to crash and refresh

See original GitHub issue

Hi, I’m having a very strange bug in my code that I can not understand.

Something simply goes wrong when I use a multipart upload fetch request. It is not a problem when uploading JSON data. When I am using multer in the middle of my action chain, my entire app occasionally crashes and refreshes, resetting user auth token and everything. I think something is wrong with the fetch request itself. It will occasionally return with no status code, and the API simply hang and not proceed to further middleware.

I had trouble using the cross-fetch polyfill for formatting multipart/form-data requests AT ALL, as it would give me an invalid BodyInit error (This went away as soon as I stopped using the polyfill…)

I have tried splitting my requests from a single endpoint which uploads user info and an image at the same time, to a chain of requests which upload JSON (success), proceed to image upload and return file path of new image (occasional success, sometimes resets and refreshes entire app), and update entry with file path JSON (PUT request, again - it never happens if there is a hang / refresh)

I am really out of ideas. The code should be correct, and works when I take out the image uploading. I am thinking of trying out different form-data libs. The fact that it would not send at all until I got rid of a fetch polyfill is extremely confusing.

Here is a gist of my code:

https://gist.github.com/Mikhail-MM/5c843ef8c77cf6d534c09f536ab0c06a

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Mikhail-MMcommented, Jul 27, 2018

@devserkan Hey, I had forgotten this issue out of frustration but saw your comment a while back and had an a-ha moment … I think you are correct, this is intended behavior of the hot-reloader when it detects that a new file has been entered into the project. I’ve noticed the same refreshing when I have added photos manually to an open React project that I am working on. I think the issue is the refresh occurring in the middle of a fetch() call which never really resolves properly, giving this error, and producing an inconsistency on whether the backend server successfully pushes that file through and does all necessary logics. Somewhat shamefully I haven’t actually tried to disable hot-reloader or even run the app in prod mode, but I believe that this issue may be solved when hot reloader is not in play as you mentioned. So good thinking 😄

0reactions
devserkancommented, Jul 27, 2018

Hello @Mikhail-MM I’m glad someone approved other than me this is because of hot reloading. Not a bug maybe but it bothers people who hit this problem 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

File upload with multer refreshes React app - Stack Overflow
This is happening because of hot-reloading comes with Creact React App. I ran into this issue too, but using plain webpack without CRA....
Read more >
Handling multipart form requests with React.js, Express API ...
In this article, we will see how to handle post requests from a multipart form in React.js and Express.js along with Axios and...
Read more >
cannot read properties of undefined (reading 'filename') multer
Multer TypeError: Cannot read property 'filename' of undefined. Asked Dec 18, 2020 • 0 votes 1 answer ... Note: I am using React.js...
Read more >
ionic Archives - LogRocket Blog
It uses native-like, web component-based UI elements and renders them in a webview component. Ionic lets developers build mobile apps with Angular, React, ......
Read more >
How to Handle File Uploads in Vue 2 | DigitalOcean
File Upload Component · Our App. · Our template has an upload form. · The form attribute enctype="multipart/form-data" is important. · We have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found