title: Error during file upload with Mongodb, Nodejs and AngularJS
body: <p>I have a problem in uploading file to a MongoDB database using AngularJS and a NodeJS REST API here is the code i tried but its not  working</p>
  
  <p>my schema:</p>
  
  <pre><code>var studentintelSchema = new Schema({
    email: {type: String, unique: true},
    natonality: {type: String},
    state: {type: String},
    lga: {type: String},
    address: {type: String},
    aboutme: {type: String},
    phonenum: {type: String, unique: true},
    student: {type: Schema.Types.ObjectId, ref: 'crtstudent'},
    avater: {type: Buffer}
  });
  
  
  module.exports = mongoose.model("studentintel", studentintelSchema);
  </code></pre>
  
  <p>An excerpt from the NodeJS API:</p>
  
  <pre><code>var storage = multer.diskStorage({
    destination: './upload/',
  });
  
  var upload = multer({ storage: storage });
  
  var sendJSONresponse = function(res, status, content) {
    res.status(status);
    res.json(content);
  };
  
  outer.post('/student/getstudentintel/:studentid', function(req, res){
    if (!req.params.studentid) {
      sendJSONresponse(res, 404, {
          "message": "Not found, studentid is required"
      });
  
      return;
    }
  
    var studentintel1 = new Studentintel();
    studentintel1.email = req.body.email;
    studentintel1.natonality = req.body.natonality;
    studentintel1.state = req.body.state;
    studentintel1.lga = req.body.lga;
    studentintel1.address = req.body.address;
    studentintel1.aboutme = req.body.aboutme;
    studentintel1.phonenum = req.body.phonenum;
    studentintel1.student = req.params.studentid;
    avater :req.files;
    studentintel1.save(function(err) {
      if (err) {
        sendJSONresponse(res, 404, err);
      } else {
        sendJSONresponse(res, 200, {
          "message": "Your profile has been saveed"
        });
      }
    });   
  });
  </code></pre>
  
  <p>An excerpt from the AngularJS client:</p>
  
  <pre><code>app.directive('fileModel', ['$parse', function ($parse) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;
  
        element.bind('change', function(){
          scope.$apply(function(){
            modelSetter(scope, element[0].files[0]);
          });
        });
      }
    };
  }]);
  
  app.service('fileUpload',  function ($http) {
    var uploadFileToUrl = function(uploadUrl, file){
      var fd = new FormData();
      for(var key in file)
        fd.append(key, file[key]);
  
      return $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
      });
    };
  
    return {
      uploadFileToUrl : uploadFileToUrl,
    };
  });
  
  app.controller('edit_profileCrl',function($scope, $http, $location, $routeParams, fileUpload){
    $scope.student = {};
  
    $scope.onSave = function(){
      fileUpload.uploadFileToUrl('/api/student/getstudentintel/' + $scope.studentid, $scope.student)
        .success(function(data) {
          $scope.formError = "";
          $scope.formSubmited = data.message;
        })
        .error(function (err) {
          $scope.formSubmited = "";
          $scope.formError = err.message;
        });
    };
  };
  </code></pre>
  
  <p>and here is my html</p>
  
  <pre><code>&lt;form&gt;
                                  &lt;div role="alert" ng-show="formError" class="alert alert-danger"&gt;{{ formError }}&lt;/div&gt;
                                  &lt;div role="alert" ng-show="formSubmited" class="alert alert-success"&gt;{{ formSubmited }}&lt;/div&gt;
                                  &lt;div class="row"&gt;
  
                                      &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;Email&lt;/label&gt;
                                              &lt;input type="email" ng-model="student.email" ng-mousedown="over()" class="form-control" placeholder="Email"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;Nationality&lt;/label&gt;
                                              &lt;input type="text" ng-model="student.natonality" ng-mousedown="over()" class="form-control" placeholder="Nationality"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
  
                                  &lt;/div&gt;
  
                                  &lt;div class="row"&gt;
  
                                      &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;State of Origin&lt;/label&gt;
                                              &lt;input type="text" ng-model="student.state" ng-mousedown="over()" class="form-control" placeholder="State of Origin"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                       &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;LGA&lt;/label&gt;
                                              &lt;input type="text" ng-model="student.lga" ng-mousedown="over()" class="form-control" placeholder="LGA"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
  
                                  &lt;/div&gt;
  
                                   &lt;div class="row"&gt;
  
                                      &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;Address&lt;/label&gt;
                                              &lt;input type="text"  ng-model="student.address" ng-mousedown="over()" class="form-control" placeholder="Address"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;Phone Number&lt;/label&gt;
                                              &lt;input type="text" ng-model="student.phonenum" ng-mousedown="over()" class="form-control" placeholder="Phone Number"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
  
                                  &lt;/div&gt;
  
                                  &lt;div class="row"&gt;
                                      &lt;div class="col-md-12"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;About Me&lt;/label&gt;
                                              &lt;textarea rows="5" ng-model="student.aboutme" ng-mousedown="over()" class="form-control" placeholder="Here can be your description"&gt;&lt;/textarea&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="row"&gt;
                                      &lt;div class="col-md-12"&gt;
                                          &lt;div class="form-group"&gt;
                                              &lt;label&gt;Avater&lt;/label&gt;
                                              &lt;input type="file"  file-model = "student.avater"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                   &lt;button ng-show="save" ng-click="onSave()" class="btn btn-info btn-fill pull-right"&gt;Save Profile&lt;/button&gt;
                                  &lt;div class="clearfix"&gt;&lt;/div&gt;
  
                              &lt;/form&gt;
  </code></pre>
  
  <p>this is all i have done but still my code is not working can any one tell me what i have done wrong</p>