#Sending FormData to express backend isnt working
37 messages · Page 1 of 1 (latest)
FRONTEND:
async function uploadForm(form) {
const data = new FormData(form);
const response = await fetch("/register", {
method: "POST",
body: JSON.stringify(data)
});
return response.json();
}
RegisterForm.addEventListener("submit", async (event) => {
event.preventDefault();
const data = await uploadForm(RegisterForm);
}
BACKEND:
app.post("/register", (req, res) => {
console.log(req.body.username) // -> undefined
res.json("Received")
});
are you sure you are sending a body value with the name "username"?
it looks like you are just sending some data to the backend
the form data
instead of
you don't need to stringify formdata
true anyways still get undefined
did you set up the body parser on your server?
yes
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
``` these are the two lines i used
body: JSON.stringify(data)
try
body: {
username: "yourdata"
}
well it should show an empty object instead of undefined 🤔
oh wait you logged .username nvm
try my fix above
it is empty though if i just do "req.body"
@desert salmon ive done that before it does work
its something wrong with my form data
yeah
but i learned you should always specify and transfer data that way
so you know where the problem lies
i just want the form data