使用Mongodb,Nodejs和AngularJS上传文件时出错

时间:2016-09-14 01:42:38

标签: javascript angularjs node.js mongoose

我在使用AngularJS将文件上传到MongoDB数据库时遇到问题,NodeJS REST API这里是我尝试的代码,但它无法正常工作

我的架构:

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

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

AngularJS客户端的摘录:

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

这是我的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>

这就是我所做的一切,但仍然我的代码不起作用,任何人都可以告诉我我做错了什么

1 个答案:

答案 0 :(得分:1)

由于API代码中的拼写错误而发生错误:

avater :req.files;

您可以通过将行更改为:

来解决此问题
studentintel1.avater = req.files;