Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created November 9, 2011 18:12
Show Gist options
  • Save aheckmann/1352317 to your computer and use it in GitHub Desktop.
Save aheckmann/1352317 to your computer and use it in GitHub Desktop.
var mongoose = require('./mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_604');
// original git://gist.github.com/1352079.git
var CalendarEvent = new Schema({
title : String,
allDay : Boolean,
start : Date,
end : Date,
url : String,
color : String,
ref : String
});
var CalendarMonth = new Schema({
id : Number,
allEvents : [CalendarEvent]
});
var CalendarEventReference = new Schema({
id : Number, //Users id
ref : [CalendarMonth]
});
var A = mongoose.model('A', CalendarEventReference);
var CalendarEvent = mongoose.model('C', CalendarEvent);
mongoose.connection.on('open', function () {
A.remove({}, function () {
A.create({ id: 1, ref: [{id:9, allEvents:[]}] }, function (err) {
if (err) return console.error(err.stack);
var theEvent = new CalendarEvent();
theEvent.title = 'my title'
theEvent.allDay = true
theEvent.start = new Date
theEvent.end = new Date
theEvent.url = "empty";
theEvent.color = 'greeeeeen'
theEvent.ref = 'the ref';
A.findOne({ id: 1}, function(err, resultReference){
if (err) return console.error(err.stack);
resultReference.ref[0].allEvents.push(theEvent);
resultReference.save(function(err){
if (err) return console.error(err.stack);
console.error('Succesfully saved');
mongoose.connection.close();
});
});
});
});
});
@guiomie
Copy link

guiomie commented Nov 9, 2011

I dont get undefined in the code I posted earlier

@aheckmann
Copy link
Author

before i rearranged the order, the event was never saved properly

@aheckmann
Copy link
Author

console.log(thing) // undefined
var thing = 'neato';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment