Node.js app.post没有填充MongoDB?

时间:2013-07-22 21:02:22

标签: javascript node.js mongodb express mongoose

我在本地运行Node.js,Express,Mongoose和Mongodb,并在我的app.js中使用此代码:

var express = require('express');
var app = express();
app.use(express.bodyParser());

// Decaler Mongoose
var mongoose = require('mongoose');
mongoose.connect('localhost','tinyreports');

// Require the Report model
var models = require('./models/reports.js');

app.post('/report/add', function(req, res) {
    var title = req.body.title;
    var recipients = req.body.recipients;

    var report = new models.Report({title:title});
    report.save( function(err, report) {
        if (err) return handleError(err);
        res.json({title:report.title});
    });

});

app.listen(3000);

console.log('server_start');

我还有以下模型(reports.js):

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var reportsSchema = new Schema({
    title:  String
});

exports.Report = mongoose.model('Report', reportsSchema);

当我运行cURL帖子curl -X POST -H "Content-Type:application/json" -d '{"title":"test"}' http://localhost:3000/report/add来填充数据库时,没有任何事情发生。

我在这里做错了什么?我的代码或整个设置有问题吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

所以,我发现它并且我的代码没有任何问题,但由于我是MongoDB的新手,我在数据库中找错了地方,因此找不到任何条目...... DOH!

答案 1 :(得分:0)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var reportsSchema = new Schema({
    title:  String
});

module.export = mongoose.model('Report', reportsSchema); 


var report = new models({title:title});
    report.save( function(err, report) {
        if (err) return handleError(err);
        res.json({title:report.title});
    }); 

change exports.report to module.export and var report =new models({})