2017-07-18 16 views

回答

0

在你的模型定義,您可以添加預處理步驟來填充你想有關係的默認值的字段。我沒有用many關係做到了這一點,但我有這樣的關係,做到了:

Report.schema.pre('save', function (next) { 
     this.wasNew = this.isNew; 
     if (!this.employee) { 
       this.employee = this.createdBy; 
     } 
     if (!this.timeEnteredTheHomeStr) { 
       if(!this.timeEnteredTheHome) { 
         this.timeEnteredTheHome = Date.now 
       } 
       this.timeEnteredTheHomeStr = String(this.timeEnteredTheHome); 
     } 
     next(); 
}); 

當有人創建了一個新的報表對象時,employee字段被自動設置爲在引用的用戶對象createdBy字段。

+0

這不是一個完整的答案給你的問題,但它是一個開始尋找的好地方。 –

相關問題