-1

this is my angular controller code where im passing certificationid and userid to delete certification details of a user.

$scope.deleteCertification = function(CertificationId){
  var userName = $scope.userId;
  var certificationId = CertificationId;
  var deleteCertificationInfo = {'userName': userName, 'certificationId':certificationId};
  console.log('deleteCertificationInfo*******');
  console.log(deleteCertificationInfo);
  userProfileService.deleteUserCertificationInfo(deleteCertificationInfo).then (function(data){
   console.log($scope.Certification);
    console.log('Certification Deleted');
  })
}

userProfileData.deleteUserCertificationInfo = function (deleteCertificationInfo) {
  var deferred = $q.defer();
  $http.delete('/api/profileUpdate/deleteUserCertification', deleteCertificationInfo, {
  }).success(function(res){
    var deletedUserCertificationResult = res;
    deferred.resolve(deletedUserCertificationResult);
    $log.debug('response from certification API:['+JSON.stringify(deletedUserCertificationResult)+']');
  }).error(function(err){
    deferred.reject(err);
  });
  return deferred.promise;
};

that is written in userProfileService to call the delete API.

but in my node controller function req.body is empty. not sure where it is going. im consoling the data in front end before sending it to service . it's displayed then. but why the req.body is getting empty?

learning developer
  • 429
  • 2
  • 6
  • 10
  • Possible duplicate of [What is a clean way to send a body with DELETE request?](http://stackoverflow.com/questions/15159213/what-is-a-clean-way-to-send-a-body-with-delete-request) – Fissio Nov 08 '16 at 09:15

2 Answers2

1

Even though you haven't posted the Express portion of your app, the best guess here is that you're not using body-parser. body-parser is an Express middleware that is required when using req.body, without adding it to your Express app, you won't be able to parse any incoming JSON or url-encoded request bodies.

const express = require('express');
const bodyParser = require('body-parser');
const port = process.env.PORT || 3000;

let app = express();

app.use(bodyParser.json()); // this will parse Content-Type: application/json 
app.use(bodyParser.urlencoded({ extended: true })); // this will parse Content-Type:  application/x-www-form-urlencoded

// Your routes go here

app.listen(port);
peteb
  • 18,552
  • 9
  • 50
  • 62
  • Check out https://docs.angularjs.org/api/ng/service/$http#delete, angular's $http delete shortcut doesn't allow to send a request body – Fissio Nov 08 '16 at 09:17
  • @Fissio You can using the `data` parameter on the config object passed in to `$http`, see the [docs](https://docs.angularjs.org/api/ng/service/$http#delete) – peteb Nov 08 '16 at 09:19
  • @UmaBankuru This is required for using `req.body` regardless of the HTTP method – peteb Nov 08 '16 at 09:25
0

try with the follwing code, its worked for me , you shoud have this code in your node service js file

app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
    extended: true
}));