I am sending form-data
along with image-data
.
But I am unable to get data on the node server when sent inside FormData
.
I could see data on the angular
controller on console.log
.
angular code:
$scope.saveInfo = function(){
var formData = new FormData;
console.log('Update function');
for(key in $scope.seller){
formData.append(key, $scope.seller[key]);
}
console.log(key, $scope.seller);
var file = $('#file')[0].files[0];
formData.append('image', file);
$http.put('/api/seller/businessInformation', formData, {
transformRequest: angular.Identity,
headers: {
'Content-type': undefined
}
}).then(function(res){
});
}
Node.Js:
I could see image data on console.log(req.files);
,
but console.log(req.body);
prints [object object].
To get started with forms, we will first install the body-parser(for parsing JSON and url-encoded data) and multer(for parsing multipart/form data) middleware. var express = require('express'); var bodyParser = require('body-parser'); var multer = require('multer'); var upload = multer(); var app = express(); app.
Yes. Node. js required for Angular 2 or Angular apps.
The best way to tackle this issue is
(i) JSON.stirngify(req.body)
in your code.
(ii) See what is the data getting printed, then based on the output you can use req.body.whateverField
EDIT
You can access FormData as,
console.log(req.files.file.name);
and to access FormData you can use
var a = JSON.parse(req.body.name);
console.log(a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With