node.js - mongoose default value equal to other value

ID : 274513

viewed : 43

Tags : node.jsmongodbnode.js





Top 3 Answer for node.js - mongoose default value equal to other value

vote vote

99

use the pre middleware explained here

userSchema.pre('save', function (next) {     this.screenname = this.get('_id'); // considering _id is input by client     next(); }); 
vote vote

88

You can pass a function to default, following is a schema field excerpt:

username: {     type: String,     required: true,     // fix for missing usernames causing validation fail     default: function() {         const _t = this as any; // tslint:disable-line         return _t.name || _t.subEmail;     } }, 
vote vote

75

Top 3 video Explaining node.js - mongoose default value equal to other value







Related QUESTION?