2016-01-19 61 views
-1

這是我controller.js的NodeJS:數據類型返回undefined

$scope.saveDetails = function(){ 
    if($scope.editMode == false){ 
    //$scope.hidesave = false; 
    //$scope.person = ""; 
    console.log("i am in save") 
    console.log($scope.person); 
    $http.post('/details',$scope.person).success(function(response){ 
     console.log(response); 
     refresh(); 
    }) 
    $state.go("itemList"); 

這是我Route.JS

module.exports = function (app){ 
    console.log("Inside Routes"); 

    app.get('/details', require('./details/details').getDetails); 
    app.get('/details/:id', require('./details/details').getDetails_id); 
    app.post('/details', require('./details/details').saveDetails); 
    app.delete('/details/:id',require('./details/details').deleteDetails); 
    app.post('/details/:id',require('./details/details').updateDetails); 
}; 

這是我details.js wher實際執行情況是:

exports.saveDetails = function (req, res) { 
    console.log("Hi"); 
    console.log("this is to save: ",req.body); 
    var person = new req.app.schema.detailsdb(req.body); 
    person.save(req.body,function(err,docs){ 
     res.json(docs); 
     console.log("docs: ",docs) 
    }); 
}; 

這裏req.body返回未定義當我嘗試從HTML頁插入數據到數據庫,我寫了使用貓鼬的數據庫

+0

你擁有包括快遞體解析器中間件? – oznu

回答

0

爲了解析JSON你需要包括快遞體分析模塊和你的路由之前使用它:

var bodyParser = require('body-parser') 

// parse application/json 
app.use(bodyParser.json()) 

// the rest of your routes here. 

app.get('/details', require('./details/details').getDetails); 
app.get('/details/:id', require('./details/details').getDetails_id); 
app.post('/details', require('./details/details').saveDetails); 
app.delete('/details/:id',require('./details/details').deleteDetails); 
app.post('/details/:id',require('./details/details').updateDetails); 

https://github.com/expressjs/body-parser/blob/master/README.md

+0

謝謝,這解決了我的錯誤...對你很重要.. :) –