Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save in Mongoose has unexpected behavior
var saveEvent = function(eventObject, userId, ref, callback){
var theEvent = new CalendarEvent();
theEvent.title = eventObject.title;
theEvent.allDay = eventObject.allDay;
theEvent.start = eventObject.start;
theEvent.end = eventObject.end;
theEvent.url = "empty";
theEvent.color = eventObject.color;
theEvent.ref = ref;
var eventDate = new Date(eventObject.start);
arrayLocation = 1 + (((parseInt(eventDate.getFullYear())) - 2011)*12) + parseInt(eventDate.getMonth());
console.log('Searching for userId in event ref collection: ' + userId);
CalendarEventReference.findOne({ id: userId }, function(err, resultReference){
if(err){
console.log('Error in finding calendar reference collection: ' + err);
callback("Cant find CalendarEventReference for user");
}
else{
resultReference.ref[arrayLocation].allEvents.push(theEvent);
resultReference.save(function(err){
if(err){
console.log('Error in finding calendar reference: ' + err);
callback("Cant save allEvents in CalendarEventReference");
}
else{
console.log('Succesfully saved Event');
callback("Successfully saved Reference");
}
});
}
});
}
---------------------------- Mongoose Schema ----------------------------------------
var CalendarEventReference = new Schema({
id : Number, //Users id
ref : [CalendarMonth]
});
var CalendarMonth = new Schema({
id : Number,
allEvents : [CalendarEvent]
});
var CalendarEvent = new Schema({
title : String,
allDay : Boolean,
start : Date,
end : Date,
url : String,
color : String,
ref : String
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.