Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to get form-data sent from angular to node server

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].

like image 553
mayank bisht Avatar asked Jan 28 '18 08:01

mayank bisht


People also ask

How do I get NodeJS form data?

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.

Does Angular run on node server?

Yes. Node. js required for Angular 2 or Angular apps.


1 Answers

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);
like image 126
Sajeetharan Avatar answered Nov 15 '22 08:11

Sajeetharan