0

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

my schema:

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);

An excerpt from the NodeJS API:

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"
      });
    }
  });   
});

An excerpt from the AngularJS client:

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;
      });
  };
};

and here is my html

<form>
                                <div role="alert" ng-show="formError" class="alert alert-danger">{{ formError }}</div>
                                <div role="alert" ng-show="formSubmited" class="alert alert-success">{{ formSubmited }}</div>
                                <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Email</label>
                                            <input type="email" ng-model="student.email" ng-mousedown="over()" class="form-control" placeholder="Email">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Nationality</label>
                                            <input type="text" ng-model="student.natonality" ng-mousedown="over()" class="form-control" placeholder="Nationality">
                                        </div>
                                    </div>

                                </div>

                                <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>State of Origin</label>
                                            <input type="text" ng-model="student.state" ng-mousedown="over()" class="form-control" placeholder="State of Origin">
                                        </div>
                                    </div>
                                     <div class="col-md-6">
                                        <div class="form-group">
                                            <label>LGA</label>
                                            <input type="text" ng-model="student.lga" ng-mousedown="over()" class="form-control" placeholder="LGA">
                                        </div>
                                    </div>

                                </div>

                                 <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Address</label>
                                            <input type="text"  ng-model="student.address" ng-mousedown="over()" class="form-control" placeholder="Address">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Phone Number</label>
                                            <input type="text" ng-model="student.phonenum" ng-mousedown="over()" class="form-control" placeholder="Phone Number">
                                        </div>
                                    </div>

                                </div>

                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>About Me</label>
                                            <textarea rows="5" ng-model="student.aboutme" ng-mousedown="over()" class="form-control" placeholder="Here can be your description"></textarea>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>Avater</label>
                                            <input type="file"  file-model = "student.avater">
                                        </div>
                                    </div>
                                </div>
                                 <button ng-show="save" ng-click="onSave()" class="btn btn-info btn-fill pull-right">Save Profile</button>
                                <div class="clearfix"></div>

                            </form>

this is all i have done but still my code is not working can any one tell me what i have done wrong

1 Answer 1

1

The error occurs because of a typo in the API code:

avater :req.files;

You can fix this by changing the line to:

studentintel1.avater = req.files;
Sign up to request clarification or add additional context in comments.

1 Comment

please is my schema ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.