Mongoose の成功のコールバックが実行されていますが、コンソール経由で Mongo と対話すると、db に何も表示されません。
データベースに何も表示されない理由を誰か助けてもらえますか?
app.js
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, mongoose = require('mongoose');
var app = express();
var TimesheetsController = require(__dirname + '/controllers/timesheets');
var UsersController = require(__dirname + '/controllers/users');
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/timesheets', TimesheetsController.list);
app.post('/timesheets', TimesheetsController.add);
app.get('/users', UsersController.list)
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
timesheet.js コントローラー
var TimesheetsModel = require('../models/timesheets');
var TimesheetsController = {
list : function(req, res){
TimesheetsModel.listTimesheetsByUser(function(){res.render('timesheets/index', { title: 'Timesheets',timesheets: {} });});
},
add : function(req, res){
TimesheetsModel.newTimesheet(10, req.body, function(err){
if(err){
res.json({'message':'failed.'})
}else{
res.json({'message':'success!'})
};
});
}
}
module.exports = TimesheetsController;
timesheet.js モデル
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var db = mongoose.createConnection('localhost', 'timetracker');
var TimesheetSchema = mongoose.Schema({
'date': Date,
'lines': [ {any: Schema.Types.Mixed} ],
'user_id': ObjectId
});
var Timesheet = db.model('Timesheet', TimesheetSchema)
// Timesheet.virtual('id')
// .get(function() {
// return this._id.toHexString();
// });
// Timesheet.pre('save', function(next) {
// this.keywords = extractKeywords(this.data);
// next();
// });
mongoose.model('Timesheet', Timesheet);
TimesheetsModel = {
listTimesheetsByUser : function(callback){
console.log('listing timesheets');
callback();
},
newTimesheet : function(user,data,callback){
var date = data.date;
var lines = data.lines;
var u = user;
console.log(lines);
var ts = new Timesheet({ "date": Date.now(),"lines":lines, "user_id":u });
ts.save(function (err) {
if (err) callback(err);
console.log('adding new timesheet'); //this is output
callback();
});
}
};
module.exports = TimesheetsModel