Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the value of a property as the default value for another property?

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?

like image 213
bncc Avatar asked Mar 30 '15 13:03

bncc


People also ask

How do you give a property a default value?

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.

What is the use of default value in the property?

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.

What is the use of default value entry required and length property?

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.


1 Answers

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.

like image 190
Talha Awan Avatar answered Oct 03 '22 19:10

Talha Awan