在 sail.js 模型中,一个模型属性默认等于另一个

One model attribute equal to the other by default in sail.js model

有没有办法在 sails.js 模型不更新之前将一个属性设置为与另一个属性相等?类似于:

username: {
    type: 'string',
    required: true
},

fullname: {
    type: 'string',
    defaultsTo: this.username
},
...

是的,您可以使用验证后回调来执行此操作。我推荐在 Validate() 之后,因为它会检查已经需要的用户名。

attributes : {
    username: {type:'string',required:true},
    fullname: {type:'string'}
},
afterValidate: function(values,next){
    if(!values.fullname) values.fullname = values.username;
    return next();
}