2016-02-02 74 views
1

當我運行這個應用程序並提交表單'console.log(request.body)'返回未定義。我是節點js的初學者。任何人都可以請解釋我做錯了什麼?表達式body-parser request.body返回undefined

這是登記表 register.jade

doctype html 
html 
    head 
     title. 
      Fonebook 
     link(rel='stylesheet', href='/assets/bootstrap/css/bootstrap.min.css') 
     script(src='https://code.jquery.com/jquery-2.2.0.min.js') 
     script(src='/assets/bootstrap/js/bootstrap.min.js') 
    body 
     div.container 
      h3. 
       The Fonebook 
      form(method="POST",class="center",id="regForm",action='/doReg',enctype="multipart/form-data") 
       div.panel.panel-info 
        div.panel-heading. 
         Registration 
        div.panel-body 
         div.col-md-offset-3.col-md-6.col-md-offset-3.form-group 
          label. 
           Profile Picture: 
          input(type='file',name='proPic') 
         div.col-md-offset-3.col-md-6.col-md-offset-3.form-group 
          label. 
           Email: 
          input.form-control(id="email", type="email", name="email", placeholder="Enter email ") 
         div.col-md-offset-3.col-md-6.col-md-offset-3.form-group 
          label. 
           Name: 
          input.form-control(id="name", type="text", name="name", placeholder="Enter name ") 
         div.col-md-offset-3.col-md-6.col-md-offset-3.form-group 
          label. 
           Password: 
          input.form-control(id="password", type="password", name="password") 
         div.col-md-offset-3.col-md-6.col-md-offset-3.form-group 
          a(href='login'). 
           Already have an account? 
        div.panel-footer 
         button.btn.btn-info(type="submit"). 
          Register 

這裏是我的app.js文件

var express = require('express'); 
var app = express(); 

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://127.0.0.1/test'); 

var uploadDir = __dirname + '/uploads'; 

var fs = require('fs'); 

var multer = require('multer'); 

var bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({extended: false})); 
app.use(bodyParser.json()); 


var conn = mongoose.connection; 

app.set('views', './views'); 
app.set('view engine', 'jade'); 

app.use('/assets', express.static(__dirname + '/public')); 

var port = process.env.PORT || 3000; 

app.all('/', function (req, res) { 
    res.render('login'); 

}) 

app.all('/register', function (req, res) { 
    res.render('register'); 
}) 

app.all('/login', function (req, res) { 
    res.render('login'); 
}) 

var storage = multer.diskStorage({ 
    destination: function (request, file, callback) { 
     callback(null, uploadDir); 
    }, 
    filename: function (request, file, callback) { 
     //console.log(file); 
     callback(null, Date.now() + '.jpg'); 
    } 
}); 
var upload = multer({storage: storage}).single('proPic'); 

app.all('/doReg', function (request, response) { 
    upload(request, response, function (err) { 
     if (err) { 
      console.log(err); 
      return; 
     } 
     //console.log(request.file); 
     response.end('Your File Uploaded'); 
    }) 
    console.log(request.body); 
}); 

app.listen(port); 
+0

但圖片上傳工作正常! –

+0

我不認爲'body-parser'支持'enctype =「multipart/form-data」' – SlashmanX

回答

0

body-parser不支持多形式。

From their README

這不處理多機構,由於其複雜的,通常大的性質。對於多機構,您可以在以下模塊感興趣:

打雜和連接,打雜

多方並連接多黨

強大

multer

看到你'已經使用multer你可以這樣訪問你的領域:

app.all('/doReg', upload, function (request, response) { 
    console.log(request.body); 
}); 
+0

謝謝你的回答。現在我刪除了body-parser,跟着你的方法,但是console.log(request.body.email); 仍然返回undefined和console.log(request.body);返回'{}'空的大括號。 –

+0

@ShafayatAlam - 如果您將文件輸入移動到表單中的最後一個字段,它是否有效?可能與此問題有關:https://github.com/expressjs/multer/issues/146 – SlashmanX

+0

我沒必要,app.all('/ doReg',上傳,功能(請求,響應){ console.log (request.body) 上傳(請求,響應函數(ERR){ 如果(ERR){ 的console.log(ERR); 回報; } 的console.log(request.file); 到Response.End ('Your File Uploaded'); console.log('Photo Uploaded'); }) }); 這個工程,但我不知道爲什麼:p –