I'm running Node.js, Express, Mongoose and Mongodb locally and have this code in my 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):
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.
What am I doing wrong here? Is there something wrong with my code or the whole set up?
Thanks!
So, I figured it out and there was nothing wrong with my code but since I'm new to MongoDB I looked in the wrong place in the database and thus couldn't find any entries...DOH!