prcbass prcbass - 6 months ago 26
HTML Question

req.body empty for textarea in http form (Using Express)

I have the following form to have the user upload a file and submit text:

form(action='/createpost' enctype="multipart/form-data" method='post' id="imgForm")
input(type='file' name='imgPath' size = "60")
br
textarea(name="postText" form="imgForm") Enter your post text here...
br
input(type='submit' value="Submit")


When I run this:

router.post('/createpost', function(req,res){
console.log("POST FORM BODY", req.body.postText);
res.end();
});


I get
undefined
in the console. How do I get the value of the input inside textarea?

Answer

Try removing the enctype="multipart/form-data" part in the form and see if it works. I think servers interpret data sent in multipart differently.

I don't know if I understand what enctype="multipart/form-data" does, but I'd use it when uploading a file from a form and the node module multiparty