I have a mongoose schema and I would like to use the value of a property as default value for another property, something like:
schemaDef = {
myProperty: {type: String, required: true}
...
otherProperty: {type: String, default: this.myProperty}
}
I thought it was possible but apparently is not, could someone confirm that and possibly give me a reference?
Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value.
The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.
1 Answer. Entry Required – if set to yes then it will be necessary for the user to insert the value in the field which means that field cannot be left blank. Default Value – A default value can be set for a field if user don't provide any value while entering the values in the table.
Though there's no built in way, you could write a few helper functions to behave it like you want. An example schema:
module.exports = function(){
var schemaDef = {
email : {type : String, required: true},
secondaryEmail : {type : String, required: true, selfDefault: "email"},
fullName : {type : String, required: true, selfDefault: {fields: ["firstName","middleName", "lastName"], space: true}},
password: {type: String, required: true},
firstName : {type : String, required: true},
middleName : {type : String, required: true},
lastName : {type : String, required: true}
};
var selfDefaults = initSelfDefaults(schemaDef);
var UserSchema = new Schema(schemaDef);
UserSchema.pre('validate', function(next) {
setSelfDefaults(this, selfDefaults)
next();
});
return mongoose.model("User", UserSchema);
};
You can place the following helper functions in other files to require and use them for all models/schemas:
// gets and returns all selfDefaults from the scehmaDef obj. Throws error if selfDefault reference is invalid
function initSelfDefaults(schemaDef){
var defaults = {};
for(var key in schemaDef){
if(schemaDef[key].selfDefault){
if(schemaDef[key].selfDefault.fields){
schemaDef[key].selfDefault.fields.forEach(function(field){
if (!schemaDef[field]){
throw new Error(field + " is an invalid selfDefault reference")
}
})
}
else if(!schemaDef[schemaDef[key].selfDefault]){
throw new Error(schemaDef[key].selfDefault + " is an invalid selfDefault reference")
}
defaults[key] = schemaDef[key].selfDefault;
delete schemaDef[key].selfDefault;
}
};
return defaults
}
// sets the selfDefaults for the fields of the document being created if the fields are empty
function setSelfDefaults(doc, defaults){
for(var key in defaults){
if(!doc[key]){
if(defaults[key].fields){
var fields = [];
for(var i=0; i<defaults[key].fields.length; i++){
if(doc[defaults[key].fields[i]]){
fields.push(doc[defaults[key].fields[i]])
}
}
doc[key] = defaults[key].space? fields.join(" ") : fields.join("");
}
else{
doc[key] = doc[defaults[key]];
}
}
}
return;
}
It doesn't work for nested schemas, but you can tweak the code to make it so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With