TransformRequest表单错误

时间:2017-05-22 09:38:40

标签: angularjs mongodb mongoose

所以我试图用节点和角度将图像上传到我的MongoDB。 出于某种原因,每当我尝试发布我得到错误时,在chrome控制台中错误指向transformResponse但我无法弄清楚问题是什么。

的index.html

<div ng-app="App">
  <div ng-controller="AppController">

    <form ng-submit="submit()">
       <input type="hidden" id="coordinates" ng-model="product.title">
       <input type="text" ng-model="product.description">
       <input type="file" id="file">
       <button>Post</button>
    </form>
    <h1>{{item.title}}</h1>
    <img ng-src="http://localhost:3000/images/{{item.image}}" alt="" />
    <h1>{{item.description}}</h1>
  </div>
</div>

  <script>

    angular.module('App', []).controller('AppController', function($scope, $http){
        $scope.product = {};
        $scope.submit = function (){
          var formData = new FormData;

          for(key in $scope.product){
            formData.append(key,$scope.product[key]);
          }

          var file = $('#file')[0].files[0];
          formData.append('image',file);
          console.log(file);
          $http.post('/',formData,{
            transformRequest: angular.identity, //<----- here
            headers: {
              'Content-Type': undefined
            }
          }).then(function(res){
            $scope.item = res.data;
          });
        }
      });

      </script>

app.js

 mongoose.connect('mongodb://localhost:27017/images');

 var Product = mongoose.model('Product',{
   title:{
     type:String
   },
   description:{
     type: String
   },
     image: String
 });

 router.post('/products',upload.any(),function(req,res,next){

   if(req.files){
     req.files.forEach(function(file){
        console.log(file);
        var filename = (new Date).valueOf()+"-"+file.originalname
        fs.rename(file.path,'public/images/'+filename,function(err){
          if(err)throw err;
          console.log('hello');
         //save to mongoose
          var product = new Product({
             title: req.body.title,
             description: req.body.description,
             image: filename
          });
         model.save(function(err,result){
           if(err){
              console.log("something went wrong...");
           }
           res.json(result);
         });
       });
     });
   }
 });
 module.exports = router;

0 个答案:

没有答案