As you know, MongooseJS has a “default” property available. For instance, if I want a Date property on my object, and I want that date to automatically default to the time at which the record is created, I would define it in the schema as:
var myObject = mongoose.Schema({
date: {type: Date, default: Date.now}
});
Now, the problem with doing this in CoffeeScript is that default is a reserved keyword in JavaScript, so the CoffeeScript compiler automatically wraps default in double quotes, so this CoffeeScript code:
myObject = mongoose.Schema
date:
type: Date
default: Date.now
is compiled as:
var myObject;
myObject = mongoose.Schema({
date: {type: Date, "default": Date.now}
});
This results in the default parameter not working as intended. Perhaps I’m missing something but everything I have tried just is not working. I shouldn’t need to manually set the date when saving the record, as the default keyword already provides this functionality.
Does anyone know how to get around this?
So, the solution to my problem was rather simple, and a rookie mistake: I forgot to specify the Date property to return in myObject.find()…