Mongodb | TypeError:无法读取未定义的属性“post”

时间:2016-05-20 22:05:29

标签: node.js mongodb express mongoose

之前有效,我只是将'server / routes / api.js'的名称改为'server / routes / capture.js' 它现在给出了这个错误。 它是如何给予未定的?

这是整个错误:

/home/ubuntu/workspace/server/routes/capture.js:3
    router.post('/captures', function(req, res){
          ^

TypeError: Cannot read property 'post' of undefined
    at module.exports (/home/ubuntu/workspace/server/routes/capture.js:3:11)
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:31:35)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

我的代码:

服务器/路由/ capture.js

var Capture = require('../models/capture');
module.exports = function(router) {
    router.post('/captures', function(req, res){
        var capture = new Capture();
        capture.birdname = req.body.birdname;
        capture.place.city = req.body.place.city;
        capture.place.country = req.body.place.country;

        capture.save(function(err, data){
            if(err)
                throw err;
            console.log(req.body);
            res.json(data);
        });
    });

    router.get('/captures', function(req, res){
        Capture.find({}, function(err, data){
            res.json(data);
        });
    });

    router.delete('/captures', function(req, res){
        Capture.remove({}, function(err){
            res.json({result: err ? 'error' : 'ok'});
        });
    });

    router.get('/captures/:id', function(req, res){
        Capture.findOne({_id: req.params.id}, function(err, data){
            res.json(data);
        });
    });

    router.delete('/captures/:id', function(req, res){
        Capture.remove({_id: req.params.id}, function(err){
            res.json({result: err ? 'error' : 'ok'});
        });
    });

    router.post('/captures/:id', function(req, res){
        Capture.findOne({_id: req.params.id}, function(err, data){
            var capture = data;
            capture.birdname = req.body.birdname;
            capture.place.city = req.body.place.city;
            capture.place.country = req.body.place.country;

            capture.save(function(err, data){
                if(err)
                    throw err;
                res.json(data);
            });
        })
    })
}

服务器/模型/ capture.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = mongoose.Schema({
    birdname: String,
    place: {
        city: String,
        country: String   
    }
});

module.exports = mongoose.model('Capture', captureSchema)

服务器/配置/ database.js

module.exports = {
    'url': "mongodb://[USERNAME]:[PASSWORD]@ds032579.mlab.com:32579/birdspotter"
}

server.js

// Init Express Web Framework
var express = require('express');
var app = express();
var path = require('path');

// Set view engine to EJS & set views directory
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, 'client', 'views'));

app.use(express.static(path.resolve(__dirname, 'client')));

// Database Connection
var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');
mongoose.connect(configDB.url);

var bodyParser = require('body-parser');
app.use(bodyParser.json());

// Main route
app.get('/', function(req, res){
    res.render('index.ejs');
});

// API 
var api = express.Router();
require('./server/routes/capture')(api);
app.use('/api', api);

// Set routes to other pages
app.get('/*', function(req, res){
    res.render('index.ejs');
});

// Port Settings
app.listen(process.env.PORT || 3000, process.env.IP);
console.log('Listening on port ' + process.env.PORT);

2 个答案:

答案 0 :(得分:0)

您的问题是您的捕获文件未通过Route。您可以通过文件中的控制台日志记录路由验证它,并且它将返回undefined。在路由文件中访问路由器的一种方法如下:

在app中

var capture = require('./capture');
app.use('/capture', capture);

在capture.js中:

var express = require('express'),
    router = express.Router(),

module.exports = function(alertService) {

    router.use(sanitize.facilityIds);

    router.post('/', function(req, res) {
        // your code here
    });
    return router;
};

答案 1 :(得分:0)

Mongo的findOne()功能可能存在问题。在较新版本的Mongo中不推荐使用findOne()。相反,尝试使用: find().item(1)