Relative Content

Tag Archive for javascriptreactjsnode.jsfilehttp-post

How to send a PDF to a backend to be saved in MongoDB

I’ve been trying to wrap my head around this problem and have looked through a lot of posts, but can’t seem to find a solution.
Essentially, I’m allowing the user to upload a PDF from their computer into the browser using an input element, adding some additional details like title, description, etc, and then I want to send that to a backend to be stored in a database.
I have started using FormData, believing that was the easiest solution based on what I’ve read on MDN and other sources, but when I .append() everything and try to send the FormData, I get back "Unexpected token '<', "<!DOCTYPE "... is not valid JSON", which I’ve known to mean that the endpoint was incorrect, however, I’ve gotten my app to fetch premade, non-file-including, get requests using the same endpoint, but 'POST' instead of 'GET', so I’m not sure the issue lies there.
On the server side, I’m logging Error: The request body was empty, meaning it’s receiving req.body, but there’s nothing in it. I’m sort of at my wits end here and believe this to be simpler than I’m making it to be. I really don’t want to use any third-party libraries (besides what’s included in React) on the front-end if that’s possible.
The backend is my own Node server for context.
Thanks in advanced.