I am not sure where the code is wrong on the client or the server? It works if I do not send a FormData (req.body reads the info) but once I changed it to FormData since I am trying to send a picture along with some strings, I get a error 500 on the server side on the title.length console.log
Req.body with my current body-parser does not extract FormData being sent back by the AJAX JS Request...
This is my JS side:
var newProjectImage = $("#filebutton").get(0).files[0];
var formData = new FormData();
formData.append('picture', newProjectImage);
formData.append('title', newProjectTitle.trim());
$.ajax({
type: "POST",
url: "/adminAddProject",
data: formData,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function (data) {
if(data) {
alert("Product added");
successAddProject(data);
enableAddProjectButtons();
} else {
alert("data does not exist ");
enableAddProjectButtons();
}
console.log("the data returned is " + JSON.stringify(data));
}
});
This is the NodeJS side:
My app.js has these for body-parse:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
Then the route is this:
router.post('/adminAddProject', function(req, res) {
console.log("Called admin add " + JSON.stringify(req.body));
if(req.cookies.token) {
console.log("Called admin token passes");
if(req.body) {
console.log("Called admin body is real ");
var title;
title = req.body.title;
console.log("Called admin body is real " + title.length);