TypeError:无法读取未定义的属性“_id”,即使包含了body-parser

时间:2015-06-28 15:17:31

标签: javascript node.js mongodb express body-parser

我正在尝试学习平均堆栈,我现在仍然坚持使用这段错误TypeError: Cannot read property '_id' of undefined我在互联网上搜索,人们说我必须包含正文解析器,我在我的服务器中.js文件,我一定做错了什么,顺便提一下,我想提一下,奇怪的是,我的应用程序中没有node_modules文件夹,但是直到body-parser,一切正常,我确实已经快速安装为全局包{{ 0}}这是我的文件server.js

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('contactlist', ['contactlist']);
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

app.get('/contactlist', function (req, res) {
    db.contactlist.find(function (err, docs) {
        console.log(docs);
        res.json(docs);
    });
});
app.post('/contactlist', function (req, res) {
    console.log(req.body);
    db.contactlist.insert(res.body,function(err,doc){
        res.json(doc);
    });
});
app.listen(3000);
console.log('Server running on port 3000');

index.html file

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Static Page</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/angular.min.js"></script>
    <script src="/js/controllers/controller.js"></script>
</head>
<body>
    <div class="container" ng-controller="AppCtrl">
        <h1>Contact List App</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Number</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="text" class="form-control" ng-model="contact.name"></td>
                    <td><input type="text" class="form-control" ng-model="contact.email"></td>
                    <td><input type="text" class="form-control" ng-model="contact.number"></td>
                    <td>
                        <button class="btn btn-primary" ng-click="addContact()">Add</button>
                    </td>
                </tr>
                <tr ng-repeat="contact in contactlist">
                    <td>{{ contact.name }}</td>
                    <td>{{ contact.email }}</td>
                    <td>{{ contact.number }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

controller.js file

var myApp = angular.module('myApp',[]);

myApp.controller('AppCtrl',['$scope','$http',function($scope,$http){
    $http.get('/contactlist').success(function(response){
        console.log("I got the data");
        $scope.contactlist = response;
    });
    $scope.addContact = function(){
        $http.post('/contactlist',$scope.contact).success(function(response){
            console.log(response);
        });
    }
}]);

1 个答案:

答案 0 :(得分:1)

您应该使用req.body而不是res.body

此外,您应该确保发送正确的Content-Type客户端。您有一个JSON正文解析中间件已添加,但您是在发送JSON POST请求还是实际发送application/x-www-url-encoded POST请求(您可以通过在POST处理程序中放置console.log(req.headers['content-type'])之类的内容进行检查)?