2016-09-09 55 views
1

我是node.js的新手。我正在嘗試創建一個註冊頁面並使用mysql將輸入的值保存在數據庫中。Node.js無法讀取屬性'名字'未定義

我可以通過在請求頭上傳遞參數來做到這一點。但是,在請求頭中傳遞參數通常不是一個好主意。所以試圖在Request body上傳遞參數。

我使用Postman來測試我的node.js服務。

這裏是GitHub的鏈接,我的演示應用

​​

我需要額外做的傳入請求體參數?

當在請求體中傳遞參數時,我得到了下面提到的錯誤。

TypeError: Cannot read property 'firstname' of undefined
   at Object.db.register (/var/projects/node_gulp/server/db.js:13:65)
   at wrapper.register (/var/projects/node_gulp/server/wrapper.js:8:12)
   at Layer.handle [as handle_request] (/var/projects/node_gulp/node_modules/express/lib/router/layer.js:95:5)
   at next (/var/projects/node_gulp/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/var/projects/node_gulp/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/var/projects/node_gulp/node_modules/express/lib/router/layer.js:95:5)
   at /var/projects/node_gulp/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/var/projects/node_gulp/node_modules/express/lib/router/index.js:330:12)
   at next (/var/projects/node_gulp/node_modules/express/lib/router/index.js:271:10)
   at Function.handle (/var/projects/node_gulp/node_modules/express/lib/router/index.js:176:3)

代碼: 服務器 - > wrapper.js

var express = require('express'); 
var db = require('./db.js'); 
var config = require('config'); 

var wrapper = { 
register: function(req, res) { 
console.log('inside wrapper'); 
db.register(req.body); 
} 
}; 
module.exports = wrapper; 

路線 - > index.js

var router = require('express').Router(); 
var wrapper = require('../server/wrapper'); 

router.post('/register', wrapper.register); 

module.exports = router; 

app.js

var express = require('express'); 
var app = express(); 
var port = process.env.PORT || 3000; 
var router = require('./routes'); 
var bodyParser = require('body-parser'); 

app.use('/', router); 

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); 

app.listen(port, function() { 
console.log('Server started at ' + port); 
}); 

服務器 - > db.js

var mysql = require('mysql'); 
var config = require('config'); 

var connection = mysql.createConnection({ 
host: config.get('database.host'), 
user: config.get('database.user'), 
password: config.get('database.password'), 
database: config.get('database.database') 
}); 
var db = { 
register: function(parameter) { 
var qry = 'insert into users values (' + "'" + parameter.firstname + "'" + ',' + "'" + parameter.lastname + "'" + ',' + "'" + parameter.username + "'" + ',' + "'" + parameter.bday + "'" + ',' + "'" + parameter.gender + "'" + ',' + "'" + parameter.password + "'" + ')'; 
console.log(qry); 
connection.connect(); 
connection.query(qry, function(err, rows, fields) { 
if (!err) { 
     console.log('The solution is: ', rows); 
     } else 
      console.log('Error while performing Query.' + err); 
    }); 

    connection.end(); 
} 
}; 
module.exports = db; 
+0

檢查你在req.body,req.query中有什麼。我在想你所有的數據都會去req.query .. –

回答

2

您需要之前,移動你的身體解析中間件你的路由,以便請求主體是執行路線處理程序之前先解析:

// app.js 

// ... 

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); 

app.use('/', router); 

// ... 
相關問題