[英]Node.js app.post does not populate MongoDB?
I'm running Node.js, Express, Mongoose and Mongodb locally and have this code in my app.js:我在本地运行 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');
I also have the following model(reports.js):我还有以下模型(reports.js):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var reportsSchema = new Schema({
title: String
});
exports.Report = mongoose.model('Report', reportsSchema);
When I run a cURL post curl -X POST -H "Content-Type:application/json" -d '{"title":"test"}' http://localhost:3000/report/add
to populate the database nothing happens.当我运行 cURL post
curl -X POST -H "Content-Type:application/json" -d '{"title":"test"}' http://localhost:3000/report/add
来填充数据库时,什么都没有发生。
What am I doing wrong here?我在这里做错了什么? Is there something wrong with my code or the whole set up?
我的代码或整个设置有问题吗?
Thanks!谢谢!
所以,我想通了,我的代码没有任何问题,但由于我是 MongoDB 的新手,我在数据库中查看了错误的位置,因此找不到任何条目...... DOH!
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({})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.