2014-12-31 109 views
-1

工作我更新從「開放」「Order.attempt_status」的文件,以「封閉」使用下面的代碼文件保存功能不是在貓鼬

AttemptSchema.pre('save', function (next) { 
    var attempt = this; 
    Order.findById(this.order).exec(function(err, order){ 
    if(err) next(err); 
    if(!order) next(new Error('could not load order with order_id:' + this.order)); 
    console.log(JSON.stringify(order)); 
    order._doc.attempt_status = attempt.attempt_status; 
    console.log(JSON.stringify(order)); 
    order.save(function(err){ 
     if(err) next(err); 
     else next(); 
    }); 
    }); 
}); 

當我調試它看到attempt_status得到改變,但更新文件不保存在數據庫中。以下是調試屏幕。

enter image description here

點擊http://i.stack.imgur.com/ByCPT.png,如果圖像是可見的。控制檯打印是

{ 
    "_id": "54a26634a4800130eb95be19", 
    "attempt_status": "open", 
    .... 
    .... 
} 

{ 
    "_id": "54a26634a4800130eb95be19", 
    "attempt_status": "closed", 
    .... 
    .... 
} 
+0

可以粘貼調試日誌作爲strings.cant查看圖像 – harryy000

+0

什麼是最好的更新方式? – raju

回答

1

不要直接修改私有_doc對象,分配模型實例的頂級屬性,使貓鼬可以跟蹤的變化,當你調用save應用它們。

order.attempt_status = attempt.attempt_status;