用mongoose Model.create(doc,cb)無效?
如題,跟蹤進不了回調,在sechma的pre save hook能跟蹤到確實執行到了pre save,但是沒有保存到數據庫。新添加了post save hook 但log中沒有輸出,說明save沒執行完成。
sechma
var mySchema = new mongoose.Schema({ employee_name: { type: String }, employee_no: { type: String }, month: { type: Number }, date: [String], lastModifyDate: Date }); mySchema .pre("save", function (next) { this.lastModifyDate = Date.now(); console.log("save one:", this); });
業務層
var createPromise = function (doc,newData) { return new Promise(function (fulfill, reject) { ...省略doc的校驗 doc為上個promise傳入的查詢結果 myModel.create({ employee_name: newData.emp_name, employee_no: newData.emp_no, month: newData.month, overtime_date: newData.addDates.rmArr(newData.rmDates) }, function (err, res) { if (err) return reject(err); return fulfill(res); }); }); <愛尬聊_百科全書>createPromise .then(function(res){...}) .catch(...)
現在跟蹤進入到create后沒有執行then或者catch中的console.log
記憶月璃 2022-07-02 11:24
確實在pre hook這里忘記執行next();導致保存中斷。在補上next()后問題解決。
