2016-03-01 41 views
3

我得到一個漫長的錯誤,當我嘗試我多形式的數據傳遞到後端:Multer - 錯誤:意外現場

Error: Unexpected field 
    at makeError (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/lib/make-error.js:12:13) 
    at wrappedFileFilter (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/index.js:39:19) 
    at Busboy.<anonymous> (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/lib/make-middleware.js:112:7) 
    at emitMany (events.js:108:13) 
    at Busboy.emit (events.js:182:7) 
    at Busboy.emit (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/node_modules/busboy/lib/main.js:31:35) 
    at PartStream.<anonymous> (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/node_modules/busboy/lib/types/multipart.js:208:13) 
    at emitOne (events.js:77:13) 
    at PartStream.emit (events.js:169:7) 
    at HeaderParser.<anonymous> (/Users/****/Documents/Programming Projects/JavaScript/Angular/Back-Yard-Brewing/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:51:16) 
    at emitOne (events.js:77:13) 

我想我知道是什麼問題,我只是不」不知道如何解決它。在我的代碼中,我使用一個服務來追加我的多部分表單,然後將其發送到服務器。之後,我不知道鏡像到達服務器時的名稱。因此,我不需要將其命名爲「圖片」,而需要將其命名爲更具體的內容。問題是,我不知道是什麼。這裏是我的代碼:

前端

  <label>Beer Name</label></br> 
      <input ng-model="recipe.alias"></br> 
      <label>Image</label></br> 
      <input type="file" file-model="recipe.image"> 
      <label>Category</label></br> 
      <select ng-options="beer.alias as beer.alias for beer in beerTypes" ng-model="recipe.selectedCategory" ng-change="filterByCategory(selectedCategory)"> 
      </select></br> 
      <label>Description</label></br> 
      <textarea maxlength="500" ng-model="recipe.description" cols="50" rows="5" class="input-box"></textarea></br> 
      <p>Character count: {{recipe.description.length}} out of 500</p> 
      <label>Instructions</label></br> 
      <textarea maxlength="1000" ng-model="recipe.instructions" cols="50" rows="5" class="input-box"></textarea></br> 
      <p> Character count: {{recipe.instructions.length}} out of 1000 (Minimum: 500)</p> 
      <button ng-click="createRecipe()" class="button-color btn btn-default">submit</button></br> 

$scope.recipe = {alias: null, selectedCategory: null, description: null, instructions: null, username: null, userID: null, image: null}; 

$scope.createRecipe = function(){ 
    authService.getUserInfo(function(user){ 
     // add the user information to the recipe 
     $scope.recipe.username = user.username; 
     $scope.recipe.userID = user._id; 

    }); 
    multipartForm.post("/createrecipe", $scope.recipe); 
} 

角服務

.service("multipartForm", ["$http", function($http){ 
    this.post = function(uploadUrl, data){ 
     var fd = new FormData(); 
     for(key in data){ 
      fd.append(key, data[key]); 
     } 
     $http.post(uploadUrl, fd, { 
      transformRequest: angular.identity, 
      headers: {"Content-Type" : undefined} 
     }); 
    } 
}]); 

服務器文件

var express = require("express"); 
var bodyParser = require("body-parser"); 
var session = require("express-session"); 
var multer = require("multer"); 
var upload = multer({ dest: "./uploads"}) 
var server = express(); 


var recipeController = require("./controllers/RecipeController") 
var favRecipeController = require("./controllers/FavRecipeController") 
var authenticationController = require('./controllers/Authentication'); 

var passportConfig = require('./config/passport'); 
var passport = require('passport'); 

//application configuration 
//resave will keep it true 

server.sessionMiddleware = session({ 
    secret   : "123iojojojoiklij", 
    resave   : true, 
    saveUninitialized : false, 
    rolling   : true, 
    cookie   : {maxAge: 60000 * 60} 
}); 
server.use(server.sessionMiddleware); 

//End Express Session Setup 

server.use(bodyParser.json()); 
server.use(bodyParser.urlencoded({ extended: true })); 
server.use(express.static(__dirname + "/public")); 


server.use(passport.initialize()); 

server.use(passport.session()); 


server.post("/createrecipe", upload.single("picture"), recipeController.createRecipe); 

所有幫助表示讚賞。

+0

multer有指定文件名和目的地的方法,你檢查過嗎? –

+0

@GandalftheWhite我不太確定我的理解。我知道,一旦它到達後端,您將該文件稱爲req.file,但是我遇到的問題是一旦它通過我的服務中的format.append,我不知道該映像的名稱是什麼。是否有文件進入你所說的? – user5854440

+0

使用multer你可以訪問發送給它的文件名,甚至給出一個新名稱。使用ng-file-upload作爲前端,它會讓你的任務變得簡單。 –

回答

1

OK,這樣的回答一個喊出了白袍巫師甘道夫和杉杉:

事實證明我的server.js,在我的崗位到服務器上,我因爲single.upload("photo")只需要爲single.upload("image"),,這就是我在前臺貼上的標籤。謝謝你的幫助。

獲得的經驗:即使您通過對象傳遞多部分表單,multer仍然將其視爲您最初標記爲的對象。

+0

好夥伴,我很高興你自己解決了它。乾杯! –